[Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl Editor.java, NONE, 1.1 GLView.java, 1.66,
Status: Pre-Alpha
Brought to you by:
henryml
From: Michael L. <he...@us...> - 2007-11-18 20:32:47
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv25490/src/net/sourceforge/bprocessor/gl Added Files: Editor.java Removed Files: GLView.java Log Message: Renamed GLView to Editor --- GLView.java DELETED --- --- NEW FILE: Editor.java --- //--------------------------------------------------------------------------------- // $Id: Editor.java,v 1.1 2007/11/18 20:32:48 henryml Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gl; import net.sourceforge.bprocessor.gl.tool.Tool; import net.sourceforge.bprocessor.gl.tool.ToolFactory; import net.sourceforge.bprocessor.gl.view.View; import net.sourceforge.bprocessor.gl.view.ViewToolbarFactory; import net.sourceforge.bprocessor.model.Camera; import net.sourceforge.bprocessor.model.Observer; import net.sourceforge.bprocessor.model.Project; import net.sourceforge.bprocessor.model.Selection; import net.sourceforge.bprocessor.gui.GUI; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.BorderLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.ActionListener; import javax.media.opengl.GLCanvas; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLDrawable; import javax.media.opengl.GLException; import javax.swing.BorderFactory; import javax.swing.JColorChooser; import javax.swing.JComboBox; import javax.swing.JSeparator; import javax.swing.JTextField; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import javax.swing.SwingUtilities; import javax.swing.ToolTipManager; import org.apache.log4j.Logger; /** * The 3D display */ public class Editor implements MouseListener, Observer { /** The logger */ private static Logger log = Logger.getLogger(Editor.class); /** GL canvas */ private GLCanvas glc; /** current event listener */ private View view; /** current working tool */ private Tool tool; /** the length parameter */ private LengthField lengthField; /** The label and combobox for the reality factor */ private FactorBox factor; private JPanel lengthPanel; private JLabel tipLabel; /** * Constuctor */ public Editor() { GLCapabilities glCap = new GLCapabilities(); glCap.setDoubleBuffered(true); glCap.setHardwareAccelerated(true); ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false); glc = new GLCanvas(glCap); log.info("Using JOGL version official"); Selection.primary().addObserver(this); Project.getInstance().addStaticObserver(this); GUI.getInstance().incrementPB(); JPanel jp = new JPanel(); jp.setLayout(new BorderLayout()); jp.add(glc, BorderLayout.CENTER); jp.setMinimumSize(new Dimension(320, 240)); jp.setPreferredSize(new Dimension(640, 480)); GUI.getInstance().incrementPB(); lengthPanel = new JPanel(); lengthPanel.setLayout(new BorderLayout()); lengthPanel.add(new JLabel("Length: "), BorderLayout.WEST); lengthField = new LengthField(); lengthPanel.add(lengthField, BorderLayout.CENTER); factor = new FactorBox(); //lengthPanel.add(factor, BorderLayout.EAST); JPanel tipPanel = new JPanel(); tipPanel.setLayout(new BorderLayout()); tipPanel.add(new JLabel("Tip: "), BorderLayout.WEST); tipLabel = new JLabel("Hello World!"); Font basicFont = tipLabel.getFont(); tipLabel.setFont(new Font(basicFont.getName(), Font.PLAIN, basicFont.getSize())); tipPanel.add(tipLabel, BorderLayout.CENTER); JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new BorderLayout()); bottomPanel.setBorder(BorderFactory.createEtchedBorder()); bottomPanel.add(lengthPanel, BorderLayout.NORTH); bottomPanel.add(new JSeparator(), BorderLayout.CENTER); bottomPanel.add(tipPanel, BorderLayout.SOUTH); jp.add(bottomPanel, BorderLayout.SOUTH); GUI.getInstance().incrementPB(); tool = ToolFactory.getFactory(this).getDefault(); setTip(tool.initialTip()); view = new View(this); (new ViewToolbarFactory()).registerToolbar(this); glc.addGLEventListener(view); GUI.getInstance().incrementPB(); glc.addMouseListener(tool); glc.addMouseMotionListener(tool); glc.addMouseWheelListener(tool); glc.addKeyListener(tool); glc.addMouseListener(this); GUI.getInstance().registerPanel(jp, GUI.SPLIT_MIDDLE); GUI.getInstance().incrementPB(); JMenu colorMenu = new JMenu("GL Colors"); ActionListener colorMenuListener = new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Grid Color")) { Color newColor = JColorChooser.showDialog(GUI.getInstance(), "Grid Color", new Color(View.getGridColor()[0], View.getGridColor()[1], View.getGridColor()[2])); if (newColor != null) { View.setGridColor(newColor.getRGBComponents(null)); } } else if (e.getActionCommand().equals("Line Color")) { Color newColor = JColorChooser.showDialog(GUI.getInstance(), "Line Color", new Color(View.getLineColor()[0], View.getLineColor()[1], View.getLineColor()[2])); if (newColor != null) { View.setLineColor(newColor.getRGBComponents(null)); } } else if (e.getActionCommand().equals("Constructor Line Color")) { Color newColor = JColorChooser.showDialog(GUI.getInstance(), "Constructor Line Color", new Color(View.getConstructorColor()[0], View.getConstructorColor()[1], View.getConstructorColor()[2])); if (newColor != null) { View.setConstructorColor(newColor.getRGBComponents(null)); } } else if (e.getActionCommand().equals("Background Color")) { Color newColor = JColorChooser.showDialog(GUI.getInstance(), "Background Color", new Color(View.getBgColor()[0], View.getBgColor()[1], View.getBgColor()[2])); if (newColor != null) { View.setBGColor(newColor.getRGBComponents(null)); } } else if (e.getActionCommand().equals("None Color")) { Color newColor = JColorChooser.showDialog(GUI.getInstance(), "None Color", new Color(View.getNoneColor()[0], View.getNoneColor()[1], View.getNoneColor()[2])); if (newColor != null) { View.setNoneColor(newColor.getRGBComponents(null)); } } else if (e.getActionCommand().equals("Default")) { View.setBGColor(View.BACKGROUND_COLOR); View.setGridColor(View.GRID_COLOR); View.setLineColor(View.STD_LINE_COLOR); View.setNoneColor(View.NONE_COLOR); View.setConstructorColor(View.CONSTRUCTOR_COLOR); } } }; JMenuItem gridItem = new JMenuItem("Grid Color"); gridItem.addActionListener(colorMenuListener); JMenuItem lineItem = new JMenuItem("Line Color"); lineItem.addActionListener(colorMenuListener); JMenuItem constructorItem = new JMenuItem("Constructor Line Color"); constructorItem.addActionListener(colorMenuListener); JMenuItem bgItem = new JMenuItem("Background Color"); bgItem.addActionListener(colorMenuListener); JMenuItem noneItem = new JMenuItem("None Color"); noneItem.addActionListener(colorMenuListener); JMenuItem defaultItem = new JMenuItem("Default"); defaultItem.addActionListener(colorMenuListener); GUI.getInstance().incrementPB(); colorMenu.add(gridItem); colorMenu.add(lineItem); colorMenu.add(constructorItem); colorMenu.add(bgItem); colorMenu.add(noneItem); colorMenu.add(defaultItem); GUI.getInstance().incrementPB(); GUI.getInstance().addMenu(colorMenu); GUI.getInstance().incrementPB(); glc.setAutoSwapBufferMode(false); repaint(); GUI.getInstance().incrementPB(); SwingUtilities.invokeLater ( new Runnable() { public void run() { GUI.getInstance().incrementPB(); GUI.getInstance().present(); } } ); } /** * Set cursor * @param cursor The cursor */ public void setCursor(Cursor cursor) { glc.setCursor(cursor); } /** * Change the tool * @param mode The new tool */ public void changeTool(int mode) { tool.cleanUp(); this.repaint(true); glc.removeMouseListener(tool); glc.removeMouseMotionListener(tool); glc.removeKeyListener(tool); if (mode == Tool.PREVIOUS_TOOL) { tool = ToolFactory.getFactory(this).get(Tool.PREVIOUS_TOOL); } else { tool = ToolFactory.getFactory(this).get(mode); } if (tool != null) { glc.addMouseListener(tool); glc.addMouseMotionListener(tool); glc.addKeyListener(tool); setCursor(tool.getCursor()); setTip(tool.initialTip()); tool.prepare(); } else { log.error("[changeMode] tool was null"); } getView().reset(); } /** * Repaint the canvas */ public void repaint() { if (Project.getInstance().getCurrentCamera().getType() == Camera.PERSPECTIVE) { disableFactor(); } else { setFactor((int)Project.getInstance().getCurrentCamera().getFactor()); enableFactor(); } repaint(false); } /** * Repaint the canvas. Will force instant repaint if given true * @param force wether or not to force reapint */ public void repaint(boolean force) { try { if (force) { glc.display(); } else { glc.repaint(); } } catch (GLException gle) { //gle.printStackTrace(); } } /** * Set the value of the length-field * @param value String */ public void setLengthValue(String value) { lengthField.setText(value); } /** * Sets the tip in the tip-panel * @param tip the tip to be displayed */ public void setTip(String tip) { tipLabel.setText(tip); } /** * * @return String */ public String getLengthValue() { return lengthField.getText(); } /** * Return the current used view * @return The current view */ public View getView() { return view; } /** * return the current tool in use * @return the tool */ public Tool getTool() { return tool; } /** * Return the GLDrawable * @return The GlDrawable */ public GLDrawable getGLDrawable() { return glc; } /** * The factor * @param i The size of the factor */ public void setFactor(int i) { factor.setFactor(i); } /** * Disable the factor field */ public void disableFactor() { lengthPanel.remove(factor); lengthPanel.revalidate(); } /** * Enable the factor field */ public void enableFactor() { lengthPanel.add(factor, BorderLayout.EAST); lengthPanel.revalidate(); } /** * Displays the popup menu at the given coordinates * @param pp the popup menu * @param x the x coordinate * @param y the y coordinate */ public void popup(JPopupMenu pp, int x, int y) { if (pp != null) { pp.show(glc, x, y); } } /** * Panel for the label and JComboBox representing the size factor */ class FactorBox extends JPanel implements ActionListener { private JComboBox choices; private JLabel label; private String[] items; /** * The Constructor construct both the label and combobox */ public FactorBox() { super(); label = new JLabel("Scale factor:"); this.add(label); items = new String[]{"1:1", "1:2", "1:5", "1:10", "1:20", "1:50", "1:100", "1:200", "1:500", "1:1000"}; choices = new JComboBox(items); choices.setEditable(true); choices.addActionListener(this); this.add(choices); } /** * Set the factor * @param i the value for the factor */ public void setFactor(int i) { String item = "1:" + i; choices.setSelectedItem(item); } /** * Handle the event of the combobox * @param The ActionEvent for the action */ public void actionPerformed(ActionEvent event) { String what = (String)choices.getSelectedItem(); if (what.matches("1:[\\d]+")) { int fac = Integer.parseInt(what.substring(2)); Project.getInstance().getCurrentCamera().setFactor(fac); } else { GUI.getInstance().alert("Wrong scale format, have to be 1:[integer]"); } } } /** * The length field class */ class LengthField extends JTextField { /** The logger */ private Logger log = Logger.getLogger(LengthField.class); /** * The constructor */ public LengthField() { super(15); } /** * set the text in the length field * do only take integer * @param d The integer */ public void setText(double d) { int mm = (int) (d * 1000.0); super.setText(Integer.toString(mm)); } /** * Set the text in the field to two commaseparated value x and y * @param x the first value * @param y the second value */ public void setText(double x, double y) { int xmm = (int) (x * 1000.0); int ymm = (int) (y * 1000.0); super.setText(Integer.toString(xmm) + ", " + Integer.toString(ymm)); } /** * Return the double in the textfield * @return The double in the field */ public double getDouble() { try { double d = Double.parseDouble(super.getText()); return (d / 1000); } catch (NumberFormatException e) { log.warn(e); } return 0; } } /** * @param arg0 Event */ public void mouseClicked(MouseEvent arg0) { } /** * @param arg0 Event */ public void mousePressed(MouseEvent arg0) { } /** * @param arg0 Event */ public void mouseReleased(MouseEvent arg0) { } /** * @param arg0 Event */ public void mouseEntered(MouseEvent arg0) { glc.requestFocusInWindow(); } /** * @param arg0 Event */ public void mouseExited(MouseEvent arg0) { } /** * Update this view * @param entity The changed entity */ public void update(Object entity) { getView().update(entity); repaint(); } } |