📄 genericobjecteditor.java
字号:
if (m_ClassType != null) { m_ObjectNames = getClassesFromProperties(); if (m_Object != null) { updateObjectNames(); updateChildPropertySheet(); } } } /** * Enables/disables the cancel button. * * @param flag true to enable cancel button, false * to disable it */ protected void setCancelButton(boolean flag) { if(m_cancelBut != null) m_cancelBut.setEnabled(flag); } /** * Opens an object from a file selected by the user. * * @return the loaded object, or null if the operation was cancelled */ protected Object openObject() { if (m_FileChooser == null) { createFileChooser(); } int returnVal = m_FileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File selected = m_FileChooser.getSelectedFile(); try { ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(selected))); Object obj = oi.readObject(); oi.close(); if (!m_ClassType.isAssignableFrom(obj.getClass())) { throw new Exception("Object not of type: " + m_ClassType.getName()); } return obj; } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Couldn't read object: " + selected.getName() + "\n" + ex.getMessage(), "Open object file", JOptionPane.ERROR_MESSAGE); } } return null; } /** * Opens an object from a file selected by the user. * * @return the loaded object, or null if the operation was cancelled */ protected void saveObject(Object object) { if (m_FileChooser == null) { createFileChooser(); } int returnVal = m_FileChooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File sFile = m_FileChooser.getSelectedFile(); try { ObjectOutputStream oo = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(sFile))); oo.writeObject(object); oo.close(); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Couldn't write to file: " + sFile.getName() + "\n" + ex.getMessage(), "Save object", JOptionPane.ERROR_MESSAGE); } } } /** * Creates the file chooser the user will use to save/load files with. */ protected void createFileChooser() { m_FileChooser = new JFileChooser(new File(System.getProperty("user.dir"))); m_FileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); } /** * Makes a copy of an object using serialization * @param source the object to copy * @return a copy of the source object */ protected Object copyObject(Object source) { Object result = null; try { SerializedObject so = new SerializedObject(source); result = so.getObject(); setCancelButton(true); } catch (Exception ex) { setCancelButton(false); System.err.println("GenericObjectEditor: Problem making backup object"); System.err.println(ex); } return result; } /** * Allows customization of the action label on the dialog. * @param newLabel the new string for the ok button */ public void setOkButtonText(String newLabel) { m_okBut.setText(newLabel); } /** * This is used to hook an action listener to the ok button * @param a The action listener. */ public void addOkListener(ActionListener a) { m_okBut.addActionListener(a); } /** * This is used to hook an action listener to the cancel button * @param a The action listener. */ public void addCancelListener(ActionListener a) { m_cancelBut.addActionListener(a); } /** * This is used to remove an action listener from the ok button * @param a The action listener */ public void removeOkListener(ActionListener a) { m_okBut.removeActionListener(a); } /** * This is used to remove an action listener from the cancel button * @param a The action listener */ public void removeCancelListener(ActionListener a) { m_cancelBut.removeActionListener(a); } /** Updates the child property sheet, and creates if needed */ public void updateChildPropertySheet() { // Update the object name displayed String className = "None"; if (m_Object != null) { className = m_Object.getClass().getName(); } m_ClassNameLabel.setText(className); // Set the object as the target of the propertysheet m_ChildPropertySheet.setTarget(m_Object); // Adjust size of containing window if possible if ((getTopLevelAncestor() != null) && (getTopLevelAncestor() instanceof Window)) { ((Window) getTopLevelAncestor()).pack(); } } } /** * Default constructor. */ public GenericObjectEditor() { this(false); } /** * Constructor that allows specifying whether it is possible * to change the class within the editor dialog. * * @param canChangeClassInDialog whether the user can change the class */ public GenericObjectEditor(boolean canChangeClassInDialog) { m_canChangeClassInDialog = canChangeClassInDialog; } /** Called when the class of object being edited changes. */ protected HierarchyPropertyParser getClassesFromProperties() { HierarchyPropertyParser hpp = new HierarchyPropertyParser(); String className = m_ClassType.getName(); String typeOptions = EDITOR_PROPERTIES.getProperty(className); if (typeOptions == null) { /* System.err.println("Warning: No configuration property found in\n" + PROPERTY_FILE + "\n" + "for " + className); */ } else { try { hpp.build(typeOptions, ", "); } catch (Exception ex) { System.err.println("Invalid property: " + typeOptions); } } return hpp; } /** * Updates the list of selectable object names, adding any new names to the list. */ protected void updateObjectNames() { if (m_ObjectNames == null) { m_ObjectNames = getClassesFromProperties(); } if (m_Object != null) { String className = m_Object.getClass().getName(); if(!m_ObjectNames.contains(className)){ m_ObjectNames.add(className); } } } /** * Sets whether the editor is "enabled", meaning that the current * values will be painted. * * @param newVal a value of type 'boolean' */ public void setEnabled(boolean newVal) { if (newVal != m_Enabled) { m_Enabled = newVal; } } /** * Sets the class of values that can be edited. * * @param type a value of type 'Class' */ public void setClassType(Class type) { m_ClassType = type; m_ObjectNames = getClassesFromProperties(); } /** * Sets the current object to be the default, taken as the first item in * the chooser */ public void setDefaultValue() { if (m_ClassType == null) { System.err.println("No ClassType set up for GenericObjectEditor!!"); return; } HierarchyPropertyParser hpp = getClassesFromProperties(); try{ if(hpp.depth() > 0){ hpp.goToRoot(); while(!hpp.isLeafReached()) hpp.goToChild(0); String defaultValue = hpp.fullValue(); setValue(Class.forName(defaultValue).newInstance()); } }catch(Exception ex){ System.err.println("Problem loading the first class: "+ hpp.fullValue()); ex.printStackTrace(); } } /** * Sets the current Object. If the Object is in the * Object chooser, this becomes the selected item (and added * to the chooser if necessary). * * @param o an object that must be a Object. */ public void setValue(Object o) { if (m_ClassType == null) { System.err.println("No ClassType set up for GenericObjectEditor!!"); return; } if (!m_ClassType.isAssignableFrom(o.getClass())) { System.err.println("setValue object not of correct type!"); return; } setObject(o); if (m_EditorComponent != null) m_EditorComponent.repaint(); updateObjectNames(); } /** * Sets the current Object. * * @param c a value of type 'Object' */ protected void setObject(Object c) { // This should really call equals() for comparison. boolean trueChange ; if (getValue() != null) { trueChange = (!c.equals(getValue())); } else trueChange = true; m_Backup = m_Object; m_Object = c; if (m_EditorComponent != null) { m_EditorComponent.updateChildPropertySheet(); } if (trueChange) { m_Support.firePropertyChange("", null, null); } } /** * Gets the current Object. * * @return the current Object */ public Object getValue() { return m_Object; } /** * Supposedly returns an initialization string to create a Object * identical to the current one, including it's state, but this doesn't * appear possible given that the initialization string isn't supposed to * contain multiple statements. * * @return the java source code initialisation string */ public String getJavaInitializationString() { return "new " + m_Object.getClass().getName() + "()"; } /** * Returns true to indicate that we can paint a representation of the * Object. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -