📄 inspector.java
字号:
if (editorClass == null) { editorClass = defaultEditorClass; } // instantiate PropertyEditor Class propertyEditorClass = null; PropertyEditor editor = null; try { propertyEditorClass = Class.forName(editorClass); editor = (PropertyEditor) propertyEditorClass.newInstance(); if (editor instanceof PropertyConsumer) { ((PropertyConsumer) editor).setProperties(marker, info); } editors.put(prop, editor); } catch (Exception e) { e.printStackTrace(); editorClass = null; } Component editorFace = null; if (editor != null && editor.supportsCustomEditor()) { editorFace = editor.getCustomEditor(); } else { editorFace = new JLabel("Does not support custom editor"); } if (editor != null) { Object propVal = props.get(prop); if (Debug.debugging("inspector")) { Debug.output("Inspector loading " + prop + "(" + propVal + ")"); } editor.setValue(propVal); } // Customized labels for each property, instead of the // abbreviated nature of the true property names. String labelMarker = marker + PropertyConsumer.LabelEditorProperty; String labelText = info.getProperty(labelMarker); if (labelText == null) { labelText = marker; } JLabel label = new JLabel(labelText + ":"); label.setHorizontalAlignment(SwingConstants.RIGHT); c.gridx = 0; c.gridy = i++; c.weightx = 0; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.EAST; gridbag.setConstraints(label, c); propertyPanel.add(label); c.gridx = 1; c.anchor = GridBagConstraints.WEST; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1f; gridbag.setConstraints(editorFace, c); propertyPanel.add(editorFace); String toolTip = (String) info.get(marker); label.setToolTipText(toolTip == null ? "No further information available." : toolTip); } // create the palette's scroll pane JScrollPane scrollPane = new JScrollPane(propertyPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); scrollPane.setBorder(null); // scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT); scrollPane.setAlignmentY(Component.TOP_ALIGNMENT); component.add(scrollPane, BorderLayout.CENTER); return component; } /** * Creates a JComponent with the properties to be changed. This component is * suitable for inclusion into a GUI. * * @param pc The property consumer to create a gui for. * @return JComponent, a panel holding the interface to set the properties. */ public JComponent createPropertyGUI(PropertyConsumer pc) { // fill variables this.propertyConsumer = pc; Properties props = new Properties(); props = pc.getProperties(props); Properties info = new Properties(); info = pc.getPropertyInfo(info); String prefix = pc.getPropertyPrefix(); return createPropertyGUI(prefix, props, info); } /** * Creates a JComponent with the properties to be changed. This component is * suitable for inclusion into a GUI. Don't use this method directly! Use * the createPropertyGUI(PropertyConsumer) instead. You will get a * NullPointerException if you use this method without setting the * PropertyConsumer in the Inspector. * * @param prefix the property prefix for the property consumer. Received * from the PropertyConsumer.getPropertyPrefix() method. Properties * that start with this prefix will have the prefix removed from the * display, so the GUI will only show the actual property name. * @param props the properties received from the * PropertyConsumer.getProperties() method. * @param info the properties received from the * PropertyConsumer.getPropertyInfo() method, containing descriptions * and any specific PropertyEditors that should be used for a * particular property named in the PropertyConsumer.getProperties() * properties. * @return JComponent, a panel holding the interface to set the properties. */ public JComponent createPropertyGUI(String prefix, Properties props, Properties info) { JComponent component = createEmbeddedPropertyGUI(prefix, props, info); JButton doneButton = null, cancelButton = null; JPanel buttons = new JPanel(); if (print) { doneButton = new JButton("Print"); cancelButton = new JButton("Quit"); } else { doneButton = new JButton("OK"); cancelButton = new JButton("Cancel"); } doneButton.addActionListener(this); doneButton.setActionCommand(doneCommand); cancelButton.addActionListener(this); cancelButton.setActionCommand(cancelCommand); buttons.add(doneButton); buttons.add(cancelButton); component.add(buttons, BorderLayout.SOUTH); component.validate(); return component; } /** * Implement the ActionListener interface. The actions registering here * should be generated by the two buttons in the Inspector GUI. */ public void actionPerformed(ActionEvent e) { final String actionCommand = e.getActionCommand(); String prefix = propertyConsumer.getPropertyPrefix(); if (actionCommand == doneCommand) {// confirmed Properties props = collectProperties(); if (!print) { if (windowSupport != null) { windowSupport.killWindow(); } propertyConsumer.setProperties(prefix, props); if (actionListener != null) { actionListener.actionPerformed(e); } } else { Collection keys = props.keySet(); Iterator it = keys.iterator(); while (it.hasNext()) { String next = (String) it.next(); System.out.println(next + "=" + props.get(next)); } } } else if (actionCommand == cancelCommand) {// canceled if (actionListener != null && actionListener != this) { actionListener.actionPerformed(e); } propertyConsumer = null; // to be garb. coll'd if (windowSupport != null) { windowSupport.killWindow(); } if (print) { System.exit(0); } } } /** * Tells the Inspector to collect the properties from the editors and set * them on its PropertyConsumer. */ public void collectAndSetProperties() { if (propertyConsumer != null) { String prefix = propertyConsumer.getPropertyPrefix(); Properties props = collectProperties(); propertyConsumer.setProperties(prefix, props); } } /** Extracts properties from textfield[]. */ public Properties collectProperties() { Properties props = new Properties(); Iterator values = editors.keySet().iterator(); while (values.hasNext()) { String key = (String) values.next(); PropertyEditor editor = (PropertyEditor) editors.get(key); if (editor != null) { String stuff = editor.getAsText(); // If it's not defined with text, don't put it in the // properties. The layer should handle this and use // its default settings. if (stuff != null && !stuff.equals("")) { props.put(key, stuff); } if (editor instanceof PropertyConsumer) { ((PropertyConsumer) editor).getProperties(props); } } } return props; } public void setPrint(boolean p) { print = p; } public boolean getPrint() { return print; } public WindowSupport getWindowSupport() { return windowSupport; } /** test cases. */ public static void main(String[] args) { Debug.init(); String name = (args.length < 1) ? "com.bbn.openmap.layer.shape.ShapeLayer" : args[0]; PropertyConsumer propertyconsumer = null; try { Class c = Class.forName(name); propertyconsumer = (PropertyConsumer) c.newInstance(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } Properties props = new Properties(), info = new Properties(); System.out.println("Inspecting " + name); String pp = name.substring(name.lastIndexOf(".") + 1); propertyconsumer.setPropertyPrefix(pp.toLowerCase()); props = propertyconsumer.getProperties(props); info = propertyconsumer.getPropertyInfo(info); Inspector inspector = new Inspector(); inspector.setPrint(true); inspector.addActionListener(inspector); inspector.inspectPropertyConsumer(propertyconsumer); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -