[Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/attrview ClassificationIdAttribute.java,
Status: Pre-Alpha
Brought to you by:
henryml
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv1422/src/net/sourceforge/bprocessor/gui/attrview Modified Files: GenericPanel.java Added Files: ClassificationIdAttribute.java ClassificationTextAttribute.java Removed Files: ClassificationAttribute.java Log Message: Fixed the classification attribute to work as intended Index: GenericPanel.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/attrview/GenericPanel.java,v retrieving revision 1.22 retrieving revision 1.23 diff -C2 -d -r1.22 -r1.23 *** GenericPanel.java 24 Oct 2006 20:38:28 -0000 1.22 --- GenericPanel.java 30 Oct 2006 00:00:02 -0000 1.23 *************** *** 184,190 **** where.add(new AttributeRow(ba)); } else if (a.getValue() instanceof Classification) { ! // Handles the classification ! ClassificationAttribute ca = new ClassificationAttribute(a); ! ca.addClassificationAttributeListener(new AttributeListener() { public void valueChanged(Attribute a) { obj.setAttributes(attributes); --- 184,190 ---- where.add(new AttributeRow(ba)); } else if (a.getValue() instanceof Classification) { ! // Handles the text part classification ! ClassificationTextAttribute catext = new ClassificationTextAttribute(a); ! catext.addClassificationAttributeListener(new AttributeListener() { public void valueChanged(Attribute a) { obj.setAttributes(attributes); *************** *** 197,201 **** } }); ! where.add(new AttributeRow(ca)); } else { log.info("[GenericPanel] Something were not implemented"); --- 197,218 ---- } }); ! where.add(new AttributeRow(catext)); ! if (a.getClassification() != null) { ! where.add(new AttributeRow(catext)); ! // Handles the id part of classification ! ClassificationIdAttribute caid = new ClassificationIdAttribute(a); ! caid.addClassificationAttributeListener(new AttributeListener() { ! public void valueChanged(Attribute a) { ! obj.setAttributes(attributes); ! if (obj instanceof Entity) { ! simpleUpdate = true; ! ((Entity)obj).changed(); ! } else if (obj instanceof Camera) { ! Project.getInstance().changed((Camera)obj); ! } ! } ! }); ! where.add(new AttributeRow(caid)); ! } } else { log.info("[GenericPanel] Something were not implemented"); *************** *** 272,276 **** this.add(ga); if (ga.attribute().isEditable() || ga instanceof LinkAttribute ! || ga instanceof ClassificationAttribute) { ga.addMouseListener(this); } --- 289,293 ---- this.add(ga); if (ga.attribute().isEditable() || ga instanceof LinkAttribute ! || ga instanceof ClassificationTextAttribute) { ga.addMouseListener(this); } --- NEW FILE: ClassificationTextAttribute.java --- //--------------------------------------------------------------------------------- // $Id: ClassificationTextAttribute.java,v 1.1 2006/10/30 00:00:02 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.ActionEvent; import java.awt.event.ActionListener; 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.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import net.sourceforge.bprocessor.model.Attribute; import net.sourceforge.bprocessor.model.Classification; /** * The ClassificationTextAttributeView */ public class ClassificationTextAttribute extends GenericAttribute implements ActionListener { /** The listeners */ private List listeners; /** The attribute this gui represents */ private Attribute attribute; /** The component */ private JComponent component; /** * Constructor for ClassificationTextAttribute * @param attribute The attribute */ public ClassificationTextAttribute(Attribute attribute) { super(BoxLayout.X_AXIS); this.attribute = attribute; this.listeners = new LinkedList(); Box column = Box.createVerticalBox(); component = Box.createHorizontalBox(); component.add(createMenu(attribute.getValue())); Box header = Box.createHorizontalBox(); header.add(Box.createHorizontalStrut(7)); header.add(component); header.add(Box.createHorizontalGlue()); column.add(Box.createRigidArea(new Dimension(70, 3))); column.add(header); column.add(Box.createRigidArea(new Dimension(70, 3))); this.add(column); } /** * Add a listener that are notified when the value is changed * @param listener The listener */ public void addClassificationAttributeListener(AttributeListener listener) { listeners.add(listener); } /** * Remove a listener * @param listener The listener */ public void removeClassificationAttributeListener(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 createMenu(Object value) { JMenuBar valueLabel = new JMenuBar(); JMenu menu, subMenu; MyMenuItem mi, submi; boolean hasChildren = false; if (attribute.getClassification() != null) { menu = new JMenu(attribute.getClassification().getName()); } else { menu = new JMenu("Classification"); } Iterator it = ((Classification) value).getChildren().iterator(); while (it.hasNext()) { Classification current = (Classification) it.next(); if (current.getChildren() != null) { hasChildren = true; } if (hasChildren) { Iterator subit = current.getChildren().iterator(); subMenu = new JMenu(current.getName()); submi = new MyMenuItem(current); submi.addActionListener(this); subMenu.add(submi); while (subit.hasNext()) { Classification subcur = (Classification) subit.next(); submi = new MyMenuItem(subcur); submi.addActionListener(this); subMenu.add(submi); } menu.add(subMenu); } else { mi = new MyMenuItem(current); mi.addActionListener(this); menu.add(mi); } } valueLabel.add(menu); return valueLabel; } /** * Return the attribute * @return The attribute */ public Attribute attribute() { return attribute; } /** * Handles the event from the classification menu * @param e is the action event */ public void actionPerformed(ActionEvent e) { MyMenuItem source = (MyMenuItem)(e.getSource()); attribute.setClassification(source.getClassification()); valueChanged(); } /** * extended version of JMenuItem that also contains a classification */ class MyMenuItem extends JMenuItem { /** the classification */ private Classification cla; /** * Constructor for a new MyMenuItem * @param classification the classification */ MyMenuItem(Classification classification) { super(classification.getName()); setClassification(classification); } /** * return the classification for the current menu item * @return the classification */ public Classification getClassification() { return cla; } /** * Sets the classification for a menu item * @param cla the classification */ public void setClassification(Classification cla) { this.cla = cla; } } /** * Not in use */ public void startEditing() { // TODO Auto-generated method stub } /** * Not in use */ public void stopEditing() { // TODO Auto-generated method stub } /** * Not in use */ public void cancelEditing() { // TODO Auto-generated method stub } } --- ClassificationAttribute.java DELETED --- --- NEW FILE: ClassificationIdAttribute.java --- //--------------------------------------------------------------------------------- // $Id: ClassificationIdAttribute.java,v 1.1 2006/10/30 00:00:02 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.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.StringTokenizer; 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.Classification; import net.sourceforge.bprocessor.model.Project; /** * The ClassificationIdAttributeView */ public class ClassificationIdAttribute extends GenericAttribute implements KeyListener, MouseListener { /** 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 ClassificationIdAttribute * @param attribute The attribute */ public ClassificationIdAttribute(Attribute attribute) { super(BoxLayout.X_AXIS); this.attribute = attribute; this.listeners = new LinkedList(); Box column = Box.createVerticalBox(); label = createLabel("Classification"); component = Box.createHorizontalBox(); if (!attribute.getClassification().getName().equalsIgnoreCase("Classification")) { component.add(createValueLabel(attribute.getClassification().getFullId())); } Box header = Box.createHorizontalBox(); header.add(Box.createHorizontalStrut(7)); header.add(label); header.add(component); header.add(Box.createHorizontalGlue()); column.add(Box.createRigidArea(new Dimension(70, 3))); column.add(header); column.add(Box.createRigidArea(new Dimension(70, 3))); this.add(column); } /** * 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 ClassificationIdAttribute * @param mouseListener the mouselistener */ public void addMouseListener(MouseListener mouseListener) { label.addMouseListener(mouseListener); component.addMouseListener(mouseListener); } /** * Remove a mouselisterner to the active elements in the ClassificationIdAttribute * @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 addClassificationAttributeListener(AttributeListener listener) { listeners.add(listener); } /** * Remove a listener * @param listener The listener */ public void removeClassificationAttributeListener(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); } } /** * Return the attribute * @return The attribute */ public Attribute attribute() { return attribute; } /** * Create a value label * @param value The value * @return The label */ private JComponent createValueLabel(Object value) { String s = ""; JLabel valueLabel; if (value instanceof String) { s = (String)value; } 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; } /** * Start editing the value by replacing the label with an editor */ public void startEditing() { if (editor == null) { component.remove(0); if (attribute.getClassification() != null && !attribute.getClassification().getName().equalsIgnoreCase("Classification")) { editor = (JTextField) createValueEditor(attribute.getClassification().getFullId()); } else { editor = (JTextField) createValueEditor(""); } editor.addKeyListener(this); 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) { Object val = attribute.getValue(); String id = editor.getText(); StringTokenizer st = new StringTokenizer(id, "."); Classification clas = Project.getInstance().getClassification(); String curid; while (st.hasMoreTokens()) { curid = st.nextToken(); Iterator it = clas.getChildren().iterator(); while (it.hasNext()) { Classification current = (Classification) it.next(); if (curid.equalsIgnoreCase(current.getId().toString())) { clas = current; } } } attribute.setClassification(clas); component.remove(editor); editor.removeKeyListener(this); editor = null; component.add(createValueLabel(clas.getFullId())); 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.removeKeyListener(this); editor = null; component.add(createValueLabel(attribute.getValue())); component.revalidate(); } } /** * Respond to the enter key by stopping editing. * @param event The KeyEvent */ public void keyPressed(KeyEvent event) { if (event.getKeyCode() == KeyEvent.VK_ENTER) { stopEditing(); } else if (event.getKeyCode() == KeyEvent.VK_ESCAPE) { cancelEditing(); } } /** * Not in use * @param event The KeyEvent */ public void keyTyped(KeyEvent event) { } /** * Not in use * @param event The KeyEvent */ public void keyReleased(KeyEvent event) { } /** * Start editing on the view * @param event The event */ public void mouseClicked(MouseEvent event) { startEditing(); } /** * 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) { } } |