📄 propertyfield.java
字号:
void slide(String val) { try { if (domain instanceof Interval) { Interval domain = (Interval)(this.domain); double d = Double.parseDouble(val); double min = domain.getMin().doubleValue(); double max = domain.getMax().doubleValue(); int i = (int)((d - min) / (max - min) * SLIDER_MAX); slider.setValue(i); } } catch (Exception e) { } } /** Returns the most recently set value. */ public String getValue() { return currentValue; } /** Constructs a PropertyField as just a writeable, empty text field. */ public PropertyField() { this(null,"",true); } /** Constructs a PropertyField as a writeable text field with the provided initial value. */ public PropertyField(String initialValue) { this(null,initialValue,true); } /** Constructs a PropertyField as a text field with the provided initial value, either writeable or not. */ public PropertyField(String initialValue, boolean isReadWrite) { this(null,initialValue,isReadWrite); } /** Constructs a labelled PropertyField as a writeable text field with the provided initial value. */ public PropertyField(String label, String initialValue) { this(label,initialValue,true); } /** Constructs a labelled PropertyField as a text field with the provided initial value, either writeable or not. */ public PropertyField(String label, String initialValue, boolean isReadWrite) { this(label,initialValue,isReadWrite, null, SHOW_TEXTFIELD); } /** Constructs a PropertyField with an optional label, an initial value, a "writeable" flag, an optional domain (for the slider and list options), and a display form (checkboxes, view buttons, text fields, sliders, or lists). <ul> <li>If show is SHOW_CHECKBOX, a checkbox will be shown (expecting "true" and "false" string values); pass in null for domain. <li>If show is SHOW_VIEWBUTTON, a view button will be shown (expecting a true object); pass in null for domain. <li>If show is SHOW_TEXTFIELD, a textfield will be shown; pass in null for domain. <li>If show is SHOW_SLIDER, both a textfield and a slider will be shown; the initialValue must be a number, and domain must be a sim.util.Interval. In this case, newValue(...) will be passed a String holding a number in the Interval range and must return a number. PropertyField will automatically make certain that the numbers are integral or real-valued; you do not need to check this so long as the Interval returns Longs or Doubles respectively. If isReadWrite is false, then the slider is not shown -- only the textfield. <li>If show is SHOW_LIST, a list will be shown; the initialValue must be an integer specifying the number in the list, and domain must be an array of Objects (strings, whatnot) or a java.util.List providing the objects in the list. In this case, newValue(...) will be passed a String holding a number; that number is the index in the list which the user has checked. newValue(...) must also return a String with the desired index for the list to be set to. */ public PropertyField(String label, String initialValue, boolean isReadWrite, Object domain, int show) { // create object setLayout(new BorderLayout()); add(optionalLabel,BorderLayout.WEST); valFieldBorder = valField.getBorder(); Insets i = valFieldBorder.getBorderInsets(valField); emptyBorder = new EmptyBorder(i.top,i.left,i.bottom,i.right); defaultColor = valField.getBackground(); valField.addKeyListener(listener); valField.addFocusListener(focusAdapter); viewButton.addActionListener(viewButtonListener); slider.addChangeListener(sliderListener); list.addActionListener(listListener); checkField.addActionListener(checkListener); // set values setValues(label, initialValue, isReadWrite, domain, show); } /* Resets a PropertyField with an optional label, an initial value, a "writeable" flag, an optional domain (for the slider and list options), and a display form (checkboxes, view buttons, text fields, sliders, or lists). <ul> <li>If show is SHOW_CHECKBOX, a checkbox will be shown (expecting "true" and "false" string values); pass in null for domain. <li>If show is SHOW_VIEWBUTTON, a view button will be shown (expecting a true object); pass in null for domain. <li>If show is SHOW_TEXTFIELD, a textfield will be shown; pass in null for domain. <li>If show is SHOW_SLIDER, both a textfield and a slider will be shown; the initialValue must be a number, and domain must be a sim.util.Interval. In this case, newValue(...) will be passed a String holding a number in the Interval range and must return a number. PropertyField will automatically make certain that the numbers are integral or real-valued; you do not need to check this so long as the Interval returns Longs or Doubles respectively. If isReadWrite is false, then the slider is not shown -- only the textfield. <li>If show is SHOW_LIST, a list will be shown; the initialValue must be an integer specifying the number in the list, and domain must be an array of Objects (strings, whatnot) or a java.util.List providing the objects in the list. In this case, newValue(...) will be passed a String holding a number; that number is the index in the list which the user has checked. newValue(...) must also return a String with the desired index for the list to be set to. */ public void setValues(String label, String initialValue, boolean isReadWrite, Object domain, int show) { this.domain = domain; removeAll(); add(optionalLabel,BorderLayout.WEST); // some conversions if (show==SHOW_SLIDER && !isReadWrite) show = SHOW_TEXTFIELD; if (domain !=null && domain.getClass().isArray()) { domain = Arrays.asList((Object[])domain); } displayState = show; switch(displayState) { case SHOW_SLIDER: JPanel p = new JPanel(); p.setLayout(new BorderLayout()); p.add(valField, BorderLayout.CENTER); if (isReadWrite && domain!=null && domain instanceof Interval) p.add(slider, BorderLayout.WEST); add(p,BorderLayout.CENTER); break; case SHOW_TEXTFIELD: add(valField, BorderLayout.CENTER); break; case SHOW_CHECKBOX: add(checkField, BorderLayout.CENTER); break; case SHOW_VIEWBUTTON: add(viewButton, BorderLayout.EAST); add(viewLabel, BorderLayout.CENTER); break; case SHOW_LIST: if (domain != null && domain instanceof java.util.List) { settingList = true; list.setEditable(false); list.setModel(new DefaultComboBoxModel(new Vector((java.util.List)domain))); add(list,BorderLayout.CENTER); list.setEnabled(isReadWrite); settingList = false; } break; default: break; } revalidate(); repaint(); currentValue = initialValue; optionalLabel.setText(label); checkField.setEnabled(isReadWrite); valField.setEditable(isReadWrite); valField.setBorder(isReadWrite? valFieldBorder : emptyBorder); this.isReadWrite = isReadWrite; setValue(currentValue); } /** Override this to be informed when a new value has been set. The return value should be the value you want the display to show instead. */ public String newValue(String newValue) { return newValue; } /** Override this to be informed when a property is to be viewed in its own inspector because the user pressed the "view" button. */ public void viewProperty() { } public void setToolTipText(String text) { super.setToolTipText(text); valField.setToolTipText(text); checkField.setToolTipText(text); optionalLabel.setToolTipText(text); viewButton.setToolTipText(text); viewLabel.setToolTipText(text); slider.setToolTipText(text); list.setToolTipText(text); } public Dimension getMinimumSize() { Dimension s = super.getMinimumSize(); s.height = valField.getMinimumSize().height; return s; } public Dimension getPreferredSize() { Dimension s = super.getPreferredSize(); s.height = valField.getPreferredSize().height; return s; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -