[Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/attrview MaterialAttribute.java,NONE,1.1
Status: Pre-Alpha
Brought to you by:
henryml
|
From: Nikolaj B. <nbr...@us...> - 2006-03-07 23:04:04
|
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5220/src/net/sourceforge/bprocessor/gui/attrview Added Files: MaterialAttribute.java Log Message: The Material attribute (not working yet) --- NEW FILE: MaterialAttribute.java --- //--------------------------------------------------------------------------------- // $Id: MaterialAttribute.java,v 1.1 2006/03/07 23:03:58 nbramsen Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gui.attrview; import java.awt.Dimension; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JTextField; import net.sourceforge.bprocessor.model.Attribute; import net.sourceforge.bprocessor.model.Surface; /** * The MaterialAttributeView */ public class MaterialAttribute extends GenericAttribute { /** The listeners */ private List listeners; /** The attribute this gui represents */ private Attribute attribute; /** The component */ private JComponent component; /** The Label */ private JLabel label; /** The editor, when this EditableAttribute is being edited */ private JTextField editor; /** * Constructor for MaterialAttribute * @param attribute The attribute */ public MaterialAttribute(Attribute attribute) { super(BoxLayout.X_AXIS); this.attribute = attribute; this.listeners = new LinkedList(); label = createLabel(attribute.getName()); Box header = Box.createHorizontalBox(); Box column = Box.createVerticalBox(); header.add(Box.createHorizontalGlue()); header.add(label); //column.add(Box.createHorizontalStrut(60)); column.add(Box.createRigidArea(new Dimension(70, 3))); column.add(header); column.add(Box.createRigidArea(new Dimension(70, 3))); //column.add(Box.createHorizontalStrut(60)); column.setMaximumSize(new Dimension(60, Short.MAX_VALUE)); this.add(column); this.add(Box.createHorizontalStrut(7)); component = Box.createHorizontalBox(); component.add(createValueLabel(attribute.getValue())); this.add(component); this.add(Box.createHorizontalGlue()); } /** * Create a label for the * @param key The key * @return A JLabel */ private JLabel createLabel(String key) { JLabel keyLabel = new JLabel(key + " :"); keyLabel.setFont(AttributeView.FONT_BOLD); return keyLabel; } /** * Add a mouselisterner to the active elements in the MaterialAttribute * @param mouseListener the mouselistener */ public void addMouseListener(MouseListener mouseListener) { label.addMouseListener(mouseListener); component.addMouseListener(mouseListener); } /** * Remove a mouselisterner to the active elements in the MaterialAttribute * @param mouseListener the mouselistener */ public void removeMouseListener(MouseListener mouseListener) { label.removeMouseListener(mouseListener); component.removeMouseListener(mouseListener); } /** * Add a listener that are notified when the value is changed * @param listener The listener */ public void addMaterialAttributeListener(AttributeListener listener) { listeners.add(listener); } /** * Remove a listener * @param listener The listener */ public void removeMaterialAttributeListener(AttributeListener listener) { listeners.remove(listener); } /** * Send valueChanged to all listeners */ protected void valueChanged() { Iterator iter = listeners.iterator(); while (iter.hasNext()) { AttributeListener current = (AttributeListener) iter.next(); current.valueChanged(attribute); } } /** * Create a value label * @param value The value * @return The label */ private JComponent createValueLabel(Object value) { String s = ""; s = ((Surface)value).getName(); JLabel valueLabel = new JLabel(s); if (attribute.isEditable()) { valueLabel.setFont(AttributeView.FONT_PLAIN); } else { valueLabel.setFont(AttributeView.FONT_ITALIC); } return valueLabel; } /** * Create a value editor * @param value The value * @return The editor */ private JComponent createValueEditor(Object value) { String s = ""; if (value instanceof String) { s = (String)value; } JTextField valueEditor = new JTextField(s); valueEditor.setFont(AttributeView.FONT_PLAIN); return valueEditor; } /** * Return the attribute * @return The attribute */ public Attribute attribute() { return attribute; } /** * Start editing the value by replacing the label with an editor */ public void startEditing() { if (attribute.isEditable()) { if (editor == null) { component.remove(0); editor = (JTextField) createValueEditor(attribute.getValue()); component.add(editor); component.revalidate(); } editor.requestFocus(); editor.selectAll(); } } /** * Stop editing the value by replacing the editor with an label, * and sending valueChanged to all listeners. */ public void stopEditing() { if (editor != null && attribute.isEditable()) { Object val = attribute.getValue(); if (val instanceof String) { attribute.setValue(editor.getText()); } component.remove(editor); editor = null; component.add(createValueLabel(attribute.getValue())); component.revalidate(); valueChanged(); } } /** * Cancels editing the value by replacing the editor with an label, * without sending any events to listeners. */ public void cancelEditing() { if (editor != null) { component.remove(editor); editor = null; component.add(createValueLabel(attribute.getValue())); component.revalidate(); } } /** * Start editing on the view * @param event The event */ public void mouseClicked(MouseEvent event) { } /** * Not in use * @param event The MouseEvent */ public void mousePressed(MouseEvent event) { } /** * Not in use * @param event The MouseEvent */ public void mouseReleased(MouseEvent event) { } /** * Not in use * @param event The MouseEvent */ public void mouseEntered(MouseEvent event) { } /** * Not in use * @param event The MouseEvent */ public void mouseExited(MouseEvent event) { } } |