protected ToolTipAttributes getSystemLookAndFeelAttributes() { Font font = UIManager.getFont("ToolTip.font"); Color textColor = UIManager.getColor("ToolTip.foreground"); Color interiorColor = UIManager.getColor("ToolTip.background"); Color outlineColor = javax.swing.UIManager.getColor("ToolTip.foreground"); double textOpacity = this.getOpacity(); double interiorOpacity = this.getOpacity(); double outlineOpacity = this.getOpacity(); double outlineWidth = this.getOutlineWidth(); Insets insets = null; Border border = UIManager.getBorder("ToolTip.border"); if (border instanceof LineBorder) // Implicitly checks for non-null. { outlineColor = ((LineBorder) border).getLineColor(); outlineWidth = ((LineBorder) border).getThickness(); } if (border != null) insets = border.getBorderInsets(null); if (font == null) font = this.getFont(); if (textColor == null) textColor = this.getTextColor(); if (interiorColor == null) interiorColor = this.getInteriorColor(); if (outlineColor == null) outlineColor = this.getOutlineColor(); if (insets == null) insets = this.getInsets(); return new ToolTipAttributes( font, textColor, interiorColor, outlineColor, textOpacity, interiorOpacity, outlineOpacity, outlineWidth, insets); }
/** * A Control that shows its parameters in a JTable. Custom bottons can be added. * * @author Wolfgang Christian * @version 1.0 */ public class OSPControl extends ControlFrame implements PropertyChangeListener, MainFrame { OSPControlTable table = new OSPControlTable(new XMLControlElement()); JScrollPane controlScrollPane = new JScrollPane(table); JTextArea messageTextArea; JLabel clearLabel, messageLabel, inputLabel; JSplitPane splitPane; JMenuItem translateItem; static final Color PANEL_BACKGROUND = javax.swing.UIManager.getColor("Panel.background"); // $NON-NLS-1$ /** * Constructs an OSPControl. * * @param _model */ public OSPControl(Object _model) { super(ControlsRes.getString("OSPControl.Default_Title")); // $NON-NLS-1$ model = _model; if (model != null) { // added by D Brown 2006-09-10 // modified by D Brown 2007-10-17 if (OSPRuntime.translator != null) OSPRuntime.translator.associate(this, model.getClass()); String name = model.getClass().getName(); setTitle( name.substring(1 + name.lastIndexOf(".")) + ControlsRes.getString("OSPControl.Controller")); // $NON-NLS-1$ //$NON-NLS-2$ } ToolsRes.addPropertyChangeListener("locale", this); Font labelFont = new Font("Dialog", Font.BOLD, 12); // $NON-NLS-1$ inputLabel = new JLabel( ControlsRes.getString("OSPControl.Input_Parameters"), SwingConstants.CENTER); // $NON-NLS-1$ inputLabel.setFont(labelFont); messageTextArea = new JTextArea(5, 5); JScrollPane messageScrollPane = new JScrollPane(messageTextArea); // contains a view of the control JPanel topPanel = new JPanel(new BorderLayout()); topPanel.add(inputLabel, BorderLayout.NORTH); topPanel.add(controlScrollPane, BorderLayout.CENTER); buttonPanel.setVisible(true); topPanel.add(buttonPanel, BorderLayout.SOUTH); // buttons are added using addButton method. // clear panel acts like a button to clear the message area JPanel clearPanel = new JPanel(new BorderLayout()); clearPanel.addMouseListener(new ClearMouseAdapter()); clearLabel = new JLabel(ControlsRes.getString("OSPControl.Clear")); // $NON-NLS-1$ clearLabel.setFont(new Font(clearLabel.getFont().getFamily(), Font.PLAIN, 9)); clearLabel.setForeground(Color.black); clearPanel.add(clearLabel, BorderLayout.WEST); // contains the messages JPanel bottomPanel = new JPanel(new BorderLayout()); messageLabel = new JLabel( ControlsRes.getString("OSPControl.Messages"), SwingConstants.CENTER); // $NON-NLS-1$ messageLabel.setFont(labelFont); bottomPanel.add(messageLabel, BorderLayout.NORTH); bottomPanel.add(messageScrollPane, BorderLayout.CENTER); bottomPanel.add(clearPanel, BorderLayout.SOUTH); Container cp = getContentPane(); splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPanel, bottomPanel); splitPane.setOneTouchExpandable(true); cp.add(splitPane, BorderLayout.CENTER); messageTextArea.setEditable(false); controlScrollPane.setPreferredSize(new Dimension(350, 200)); controlScrollPane.setMinimumSize(new Dimension(0, 50)); messageScrollPane.setPreferredSize(new Dimension(350, 75)); if (OSPRuntime.translator != null && model != null) { OSPRuntime.translator.associate(table, model.getClass()); } Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); setLocation( (d.width - getSize().width) / 2, (d.height - getSize().height) / 2); // center the frame init(); } /** * Gets this frame. Implementation of MainFrame interface. * * @return OSPFrame */ public OSPFrame getMainFrame() { return this; } /** * Adds a Display menu to the menu bar. Overrides OSPFrame method. * * @return the display menu */ protected JMenu loadDisplayMenu() { JMenuBar menuBar = getJMenuBar(); if (menuBar == null) { return null; } JMenu menu = super.loadDisplayMenu(); translateItem = new JMenuItem(); translateItem.setText(ControlsRes.getString("OSPControl.Translate")); // $NON-NLS-1$ // changed by D Brown 2007-10-17 if (OSPRuntime.translator != null) { translateItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { OSPRuntime.translator.showProperties(model.getClass()); if (OSPRuntime.translator instanceof Hidable) ((Hidable) OSPRuntime.translator).setKeepHidden(false); OSPRuntime.translator.setVisible(true); } }); translateItem.setEnabled(OSPRuntime.isAuthorMode()); languageMenu.add(translateItem, 0); } // changed by D Brown 2006-09-10 if (languageMenu.getItemCount() > 1) languageMenu.insertSeparator(1); return menu; } /** Refreshes the user interface in response to display changes such as Language. */ protected void refreshGUI() { super.refreshGUI(); messageLabel.setText(ControlsRes.getString("OSPControl.Messages")); clearLabel.setText(ControlsRes.getString("OSPControl.Clear")); inputLabel.setText(ControlsRes.getString("OSPControl.Input_Parameters")); table.refresh(); } /** * Listens for property change events. * * @param e the property change event */ public void propertyChange(PropertyChangeEvent e) { String name = e.getPropertyName(); if (name.equals("translation") || name.equals("locale")) refreshGUI(); // forward event to other listeners else firePropertyChange(e.getPropertyName(), e.getOldValue(), e.getNewValue()); } /** * Initializes this control after all objects have been created. * * <p>Override this method and change the default close operation if this control is used with an * applet. */ protected void init() { splitPane.setDividerLocation(-1); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public Object getModel() { return model; } /** * Sets the location of the divider between the control table and the message panel. * * @param loc int */ public void setDividerLocation(int loc) { splitPane.setDividerLocation(loc); } /** * Sets the editable property of the given parameter so that it can not be changed from within the * GUI. * * @param parameter String * @param editable boolean */ public void setEditable(String parameter, boolean editable) { table.setEditable(parameter, editable); } /** * Locks the control's interface. Values sent to the control will not update the display until the * control is unlocked. * * @param lock boolean */ public void setLockValues(boolean lock) { table.setLockValues(true); } /** * Creates a string representation of the control parameters. * * @return the control parameters */ public String toString() { return table.toString(); } /** * Adds a parameter to the input display. * * @param par the parameter name * @param val the initial parameter value */ public void setValue(String par, Object val) { table.setValue(par, val); } /** * Adds an initial boolean value of a parameter to the input display. * * @param par the parameter name * @param val the initial parameter value */ public void setValue(String par, boolean val) { table.setValue(par, val); } /** * Adds an initial value of a parameter to the input display. * * @param par the parameter name * @param val the initial parameter value */ public void setValue(String par, double val) { table.setValue(par, Double.toString(val)); } /** * Adds an initial value of a parameter to the input display. * * @param par the parameter name * @param val the initial parameter value */ public void setValue(String par, int val) { table.setValue(par, Integer.toString(val)); } /** * Removes a parameter from the table. * * @param par the parameter name */ public void removeParameter(String par) { table.setValue(par, null); } /** * Reads a parameter value from the input display. * * @param par * @return double the value of of the parameter */ public double getDouble(String par) { return table.getDouble(par); } /** * Reads a parameter value from the input display. * * @param par * @return int the value of of the parameter */ public int getInt(String par) { return table.getInt(par); } /** * Gets the object with the specified property name. Throws an UnsupportedOperationException if * the named object has not been stored. * * @param par * @return the object */ public Object getObject(String par) throws UnsupportedOperationException { return table.getObject(par); } /** * Reads a parameter value from the input display. * * @param par the parameter name * @return String the value of of the parameter */ public String getString(String par) { return table.getString(par); } /** * Reads a parameter value from the input display. * * @param par the parameter name * @return the value of of the parameter */ public boolean getBoolean(String par) { return table.getBoolean(par); } /** * Reads the current property names. * * @return the property names */ public Collection getPropertyNames() { return table.getPropertyNames(); } /** * Adds a custom button to the control's frame. * * @param methodName the name of the method; the method has no parameters * @param text the button's text label * @return the custom button */ public JButton addButton(String methodName, String text) { return addButton(methodName, text, null, model); } /** * Adds a custom button to the control's frame. * * @param methodName the name of the method; the method has no parameters * @param text the button's text label * @param toolTipText the button's tool tip text * @return the custom button */ public JButton addButton(String methodName, String text, String toolTipText) { return addButton(methodName, text, toolTipText, model); } /** * Adds a ControlTableListener that invokes method in the control's model. The method in the model * is invoked with the table's variable name passed as a parameter. * * @param methodName the name of the method; the method has no parameters */ public void addControlListener(String methodName) { addControlListener(methodName, model); } /** * Adds a ControlTableListener that invokes method in the given object. The method in the target * is invoked with the table's variable name passed as a parameter. * * @param methodName the name of the method; the method has no parameters * @param target the target for the method */ public void addControlListener(String methodName, final Object target) { Class[] parameters = new Class[] {String.class}; try { final java.lang.reflect.Method m = target.getClass().getMethod(methodName, parameters); table.tableModel.addTableModelListener( new TableModelListener() { public void tableChanged(TableModelEvent e) { if (e.getType() != TableModelEvent.UPDATE || e.getColumn() != 1 || e.getFirstRow() < 0) { return; } String name = table.getValueAt(e.getFirstRow(), 0).toString(); Object[] args = {name}; try { m.invoke(target, args); } catch (IllegalAccessException iae) { } catch (java.lang.reflect.InvocationTargetException ite) { } } }); } catch (NoSuchMethodException nsme) { System.err.println( "The method " + methodName + "() does not exist."); // $NON-NLS-1$ //$NON-NLS-2$ } } /** * Prints a line of text in the message area. * * @param s */ public void println(String s) { print(s + "\n"); // $NON-NLS-1$ } /** Prints a blank line in the message area. */ public void println() { print("\n"); // $NON-NLS-1$ } /** * Prints text in the message area. * * @param s */ public void print(final String s) { if (SwingUtilities.isEventDispatchThread() || Thread.currentThread().getName().equals("main")) { // $NON-NLS-1$ messageTextArea.append(s); return; } Runnable doLater = new Runnable() { public void run() { messageTextArea.append(s); } }; // Update from the event queue. java.awt.EventQueue.invokeLater(doLater); } /** Remove all text from the message area. */ public void clearMessages() { if (SwingUtilities.isEventDispatchThread() || Thread.currentThread().getName().equals("main")) { // $NON-NLS-1$ messageTextArea.setText(""); // $NON-NLS-1$ return; } Runnable doLater = new Runnable() { public void run() { messageTextArea.setText(""); // $NON-NLS-1$ } }; // Update from the event queue. java.awt.EventQueue.invokeLater(doLater); } /** Remove all text from the data input area. */ public void clearValues() { table.clearValues(); } /** * A signal that a method has completed. A message is printed in the message area. * * @param message */ public void calculationDone(String message) { // not implemented if (message != null) println(message); } class ClearMouseAdapter extends java.awt.event.MouseAdapter { /** * Method mousePressed * * @param evt */ public void mousePressed(java.awt.event.MouseEvent evt) { clearMessages(); } /** * Method mouseEntered * * @param evt */ public void mouseEntered(java.awt.event.MouseEvent evt) { clearLabel.setFont(new Font(clearLabel.getFont().getFamily(), Font.BOLD, 10)); clearLabel.setText(ControlsRes.getString("OSPControl.Click_to_clear_message")); // $NON-NLS-1$ } /** * Method mouseExited * * @param evt */ public void mouseExited(java.awt.event.MouseEvent evt) { clearLabel.setFont(new Font(clearLabel.getFont().getFamily(), Font.PLAIN, 9)); clearLabel.setText(ControlsRes.getString("OSPControl.Clear")); // $NON-NLS-1$ } } /** * Returns an XML.ObjectLoader to save and load data for this object. * * @return the object loader */ public static XML.ObjectLoader getLoader() { return new OSPControlLoader(); } /** A class to save and load data for OSPControls. */ static class OSPControlLoader implements XML.ObjectLoader { /** * Saves object data to an XMLControl. * * @param control the control to save to * @param obj the object to save */ public void saveObject(XMLControl xmlControl, Object obj) { OSPControl ospControl = (OSPControl) obj; saveControlProperites(xmlControl, ospControl); // save the model if the control is the top level if (xmlControl.getLevel() == 0) { xmlControl.setValue("model", ospControl.model); // $NON-NLS-1$ } } protected void saveControlProperites(XMLControl xmlControl, OSPControl ospControl) { // save the parameters Iterator it = ospControl.getPropertyNames().iterator(); while (it.hasNext()) { String name = (String) it.next(); Object val = ospControl.getObject(name); if (val.getClass() == DoubleArray.class) { xmlControl.setValue(name, ((DoubleArray) val).getArray()); } else if (val.getClass() == IntegerArray.class) { xmlControl.setValue(name, ((IntegerArray) val).getArray()); } else if (val.getClass() == Boolean.class) { xmlControl.setValue(name, ((Boolean) val).booleanValue()); } else if (val.getClass() == Double.class) { xmlControl.setValue(name, ((Double) val).doubleValue()); } else if (val.getClass() == Integer.class) { xmlControl.setValue(name, ((Integer) val).intValue()); } else if (val.getClass().isArray()) { xmlControl.setValue(name, val); } else { xmlControl.setValue(name, val); } // xmlControl.setValue(name, val.toString()); } } /** * Creates an object using data from an XMLControl. * * @param control the control * @return the newly created object */ public Object createObject(XMLControl control) { return new OSPControl(null); } /** * Loads an object with data from an XMLControl. * * @param control the control * @param obj the object * @return the loaded object */ public Object loadObject(XMLControl control, Object obj) { OSPControl cf = (OSPControl) obj; // iterate over properties and add them to table model Iterator it = control.getPropertyNames().iterator(); cf.table.setLockValues(true); while (it.hasNext()) { String name = (String) it.next(); // skip "model" object properties if (name.equals("model") && control.getPropertyType(name).equals("object")) { XMLControl child = control.getChildControl("model"); // $NON-NLS-1$ cf.model = child.loadObject(cf.model); continue; } if (control.getPropertyType(name).equals("string")) { // $NON-NLS-1$ cf.setValue(name, control.getString(name)); } else if (control.getPropertyType(name).equals("int")) { // $NON-NLS-1$ cf.setValue(name, control.getInt(name)); } else if (control.getPropertyType(name).equals("double")) { // $NON-NLS-1$ cf.setValue(name, control.getDouble(name)); } else if (control.getPropertyType(name).equals("boolean")) { // $NON-NLS-1$ cf.setValue(name, control.getBoolean(name)); } else { cf.setValue(name, control.getObject(name)); } } cf.table.setLockValues(false); // XMLControl child = control.getChildControl("model"); //$NON-NLS-1$ // if(child!=null) { // cf.model = child.loadObject(cf.model); // } return obj; } } /** * Creates an OSP control and establishes communication between the control and the model. * * <p>Custom buttons are usually added to this control to invoke actions in the model. * * @param model Object * @return AnimationControl */ public static OSPControl createApp(Object model) { OSPControl control = new OSPControl(model); control.setSize(300, 300); control.setVisible(true); return control; } }
public LabelRenderer() { setHorizontalAlignment(SwingConstants.RIGHT); setOpaque(true); // make background visible. setForeground(Color.black); setBackground(javax.swing.UIManager.getColor("Panel.background")); // $NON-NLS-1$ }