📄 promptat.java
字号:
if (customButtonClicked != null) value = customButtonClicked; } } dispose(); } /** * Method called to complete a field when the dialog closes. * @param field the field to process. */ private void finishField(Field field) { if (field == null) return; switch (field.type) { case Field.FIELD_STRING: field.finalValue = field.textField.getText(); break; case Field.FIELD_SELECT: field.finalValue = field.combo.getSelectedItem(); break; case Field.FIELD_COLOR: Color newColor = parseColor(field); if (newColor == null) break; field.finalValue = newColor; break; } } /** * Method called to initialize the prompt dialog. * @param wnd the EditWindow_ in which to show the dialog. * @param ni the NodeInst (in the EditWindow_) over which to show the dialog. * @param title the title of the dialog. * @param fieldList if not null, a 1-dimensional array of fields to place in the dialog. * @param fieldArray if not null, a 2-dimensional grid of fields to place in the dialog. */ private void initComponents(EditWindow_ wnd, NodeInst ni, String title, Field [] fieldList, Field [][] fieldArray) { getContentPane().setLayout(new GridBagLayout()); setTitle(title); setName(""); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { closed = true; exit(false); } }); int buttonRow = 1; goodClicked = false; JComponent centerIt = null; this.fieldList = fieldList; this.fieldArray = fieldArray; if (fieldList != null) { for(int i=0; i<fieldList.length; i++) { centerIt = initializeField(fieldList[i], i, 0, centerIt); } buttonRow = fieldList.length + 1; } else if (fieldArray != null) { for(int i=0; i<fieldArray.length; i++) { Field [] row = fieldArray[i]; for(int j=0; j<row.length; j++) centerIt = initializeField(row[j], i, j, centerIt); } buttonRow = fieldArray.length + 1; } if (centerIt == null) centerIt = fieldList[0].labelObj; String badButton = (yesNo ? "No" : "Cancel"); JButton cancel = new JButton(badButton); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = buttonRow; gbc.insets = new java.awt.Insets(4, 4, 4, 4); gbc.weightx = 0.5; getContentPane().add(cancel, gbc); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { exit(false); } }); String goodButton = (yesNo ? "Yes" : "OK"); JButton ok = new JButton(goodButton); getRootPane().setDefaultButton(ok); gbc = new java.awt.GridBagConstraints(); gbc.gridx = 1; gbc.gridy = buttonRow; gbc.gridwidth = 3; gbc.insets = new java.awt.Insets(4, 4, 4, 4); gbc.weightx = 0.5; getContentPane().add(ok, gbc); ok.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exit(true); } }); pack(); // now make the dialog appear over a node Point ew = wnd.getScreenLocationOfCorner(); Point locInWnd = wnd.databaseToScreen(ni.getAnchorCenterX(), ni.getAnchorCenterY()); Point textfield = centerIt.getLocation(); Dimension textSize = centerIt.getSize(); setLocation(locInWnd.x+ew.x-(textfield.x+textSize.width/2), locInWnd.y+ew.y-(textfield.y+textSize.height/2+20)); } /** * Method to initialize a field in the dialog. * @param field the Field to initialize. * @param i the Y index (0-based) of the field in the dialog. * @param j the X index (0-based) of the field in the dialog. * @param centerIt the component in the dialog that will be centered over the desired piece of circuitry. * @return the new component in the dialog that will be centered over the desired piece of circuitry. */ private JComponent initializeField(Field field, int i, int j, JComponent centerIt) { if (field == null) return centerIt; if (field.label != null) { field.labelObj = new JLabel(field.label); GridBagConstraints gbc = new java.awt.GridBagConstraints(); gbc.gridx = j*4; gbc.gridy = i; gbc.insets = new java.awt.Insets(4, 4, 4, 4); gbc.anchor = java.awt.GridBagConstraints.WEST; getContentPane().add(field.labelObj, gbc); } switch (field.type) { case Field.FIELD_STRING: field.textField = new JTextField((String)field.initial); GridBagConstraints gbc = new java.awt.GridBagConstraints(); gbc.gridx = j*4+1; gbc.gridy = i; gbc.gridwidth = 3; gbc.weightx = 1.0; gbc.fill = java.awt.GridBagConstraints.HORIZONTAL; gbc.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(field.textField, gbc); if (centerIt == null) { field.textField.selectAll(); centerIt = field.textField; } break; case Field.FIELD_SELECT: field.combo = new JComboBox(); String [] poss = (String [])field.initial; for(int k=0; k<poss.length; k++) field.combo.addItem(poss[k]); field.combo.setSelectedItem(field.finalValue); gbc = new java.awt.GridBagConstraints(); gbc.gridx = j*4+1; gbc.gridy = i; gbc.gridwidth = 3; gbc.fill = java.awt.GridBagConstraints.HORIZONTAL; gbc.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(field.combo, gbc); if (centerIt == null) centerIt = field.combo; break; case Field.FIELD_BUTTON: JButton but = (JButton)field.initial; but.addActionListener(new CustomButtonActionListener(this, field)); gbc = new java.awt.GridBagConstraints(); gbc.gridx = j*4+1; gbc.gridy = i; gbc.gridwidth = 3; gbc.fill = java.awt.GridBagConstraints.HORIZONTAL; gbc.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(but, gbc); if (centerIt == null) centerIt = but; break; case Field.FIELD_COLOR: Color col = (Color)field.initial; field.patch = new JPanel(); Dimension size = new Dimension(25, 25); field.patch.setSize(size); field.patch.setPreferredSize(size); field.patch.setBackground(col); gbc = new java.awt.GridBagConstraints(); gbc.gridx = j*4+1; gbc.gridy = i; gbc.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(field.patch, gbc); field.textField = new JTextField(); field.textField.setColumns(8); field.textField.setText(col.getRed() + "," + col.getGreen() + "," + col.getBlue()); gbc = new java.awt.GridBagConstraints(); gbc.gridx = j*4+2; gbc.gridy = i; gbc.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(field.textField, gbc); field.textField.getDocument().addDocumentListener(new ColorDocumentListener(field)); field.but = new JButton("Set"); gbc = new java.awt.GridBagConstraints(); gbc.gridx = j*4+3; gbc.gridy = i; gbc.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(field.but, gbc); field.but.addActionListener(new MixColorActionListener(this, field)); if (centerIt == null) { field.textField.selectAll(); centerIt = field.but; } break; } return centerIt; } /** * Method to extract a Color value from a Field. * @param field the Field to examine (must hold a Color selection). * @return the Color in that Field. */ private static Color parseColor(Field field) { String newColor = field.textField.getText(); String [] rgb = newColor.split(","); if (rgb.length < 3) return null; int r = TextUtils.atoi(rgb[0]); if (r < 0) r = 0; if (r > 255) r = 255; int g = TextUtils.atoi(rgb[1]); if (g < 0) g = 0; if (g > 255) g = 255; int b = TextUtils.atoi(rgb[2]); if (b < 0) b = 0; if (b > 255) b = 255; return new Color(r, g, b); } /** * Class to handle clicks on the "set" color button. */ private static class MixColorActionListener implements ActionListener { private PromptAt top; private Field field; private MixColorActionListener(PromptAt top, Field field) { this.top = top; this.field = field; } public void actionPerformed(ActionEvent evt) { Color origColor = parseColor(field); Color newColor = JColorChooser.showDialog(top, "Edit color", origColor); if (newColor == null) return; field.textField.setText(newColor.getRed() + "," + newColor.getGreen() + "," + newColor.getBlue()); field.patch.setBackground(newColor); } } /** * Class to handle clicks on user's custom buttons. */ private static class CustomButtonActionListener implements ActionListener { private PromptAt top; private Field field; private CustomButtonActionListener(PromptAt top, Field field) { this.top = top; this.field = field; } public void actionPerformed(ActionEvent evt) { top.customButtonClicked = (String)field.finalValue; top.exit(true); } } /** * Class to handle changes to the text description of a color value. */ private static class ColorDocumentListener implements DocumentListener { private Field which; private ColorDocumentListener(Field which) { this.which = which; } public void changedUpdate(DocumentEvent e) { updatePatchColor(); } public void insertUpdate(DocumentEvent e) { updatePatchColor(); } public void removeUpdate(DocumentEvent e) { updatePatchColor(); } private void updatePatchColor() { Color newColor = parseColor(which); which.patch.setBackground(newColor); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -