|
From: <sa...@us...> - 2003-12-09 12:22:07
|
Update of /cvsroot/jrobin/src/org/jrobin/inspector In directory sc8-pr-cvs1:/tmp/cvs-serv13997/org/jrobin/inspector Added Files: EditArchiveDialog.java EditDatasourceDialog.java Util.java Log Message: Minor changes --- NEW FILE: EditArchiveDialog.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.jrobin.org * Project Lead: Sasa Markovic (sa...@jr...); * * (C) Copyright 2003, by Sasa Markovic. * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * This library is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */ package org.jrobin.inspector; import org.jrobin.core.RrdException; import org.jrobin.core.ArcDef; import javax.swing.*; import java.awt.*; import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; class EditArchiveDialog extends JDialog { private static final int FIELD_SIZE = 20; private static final String TITLE_NEW = "New archive"; private static final String TITLE_EDIT = "Edit archive"; private JLabel consolFunLabel = new JLabel("Consolidation function: "); private JLabel xffLabel = new JLabel("X-files factor: "); private JLabel stepsLabel = new JLabel("Steps: "); private JLabel rowsLabel = new JLabel("Rows: "); private JComboBox consolFunCombo = new JComboBox(); private JTextField xffField = new JTextField(FIELD_SIZE); private JTextField stepsField = new JTextField(FIELD_SIZE); private JTextField rowsField = new JTextField(FIELD_SIZE); private JButton okButton = new JButton("OK"); private JButton cancelButton = new JButton("Cancel"); private ArcDef arcDef; EditArchiveDialog(Frame parent, ArcDef arcDef) { super(parent, arcDef == null? TITLE_NEW: TITLE_EDIT, true); constructUI(arcDef); pack(); Util.centerOnScreen(this); setVisible(true); } private void constructUI(ArcDef arcDef) { // fill controls String[] funs = ArcDef.CONSOL_FUNS; for (int i = 0; i < funs.length; i++) { consolFunCombo.addItem(funs[i]); } consolFunCombo.setSelectedIndex(0); if(arcDef == null) { // NEW xffField.setText("" + 0.5); } else { // EDIT consolFunCombo.setSelectedItem(arcDef.getConsolFun()); consolFunCombo.setEnabled(false); xffField.setText("" + arcDef.getXff()); stepsField.setText("" + arcDef.getSteps()); stepsField.setEnabled(false); rowsField.setText("" + arcDef.getRows()); // rowsField.setEnabled(false); } // layout JPanel content = (JPanel) getContentPane(); GridBagLayout layout = new GridBagLayout(); content.setLayout(layout); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(3, 3, 3, 3); gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.EAST; layout.setConstraints(consolFunLabel, gbc); content.add(consolFunLabel); gbc.gridy = 1; layout.setConstraints(xffLabel, gbc); content.add(xffLabel); gbc.gridy = 2; layout.setConstraints(stepsLabel, gbc); content.add(stepsLabel); gbc.gridy = 3; layout.setConstraints(rowsLabel, gbc); content.add(rowsLabel); gbc.gridy = 4; layout.setConstraints(okButton, gbc); okButton.setPreferredSize(cancelButton.getPreferredSize()); content.add(okButton); gbc.gridx = 1; gbc.gridy = 0; gbc.anchor = GridBagConstraints.WEST; layout.setConstraints(consolFunCombo, gbc); content.add(consolFunCombo); gbc.gridy = 1; layout.setConstraints(xffField, gbc); content.add(xffField); gbc.gridy = 2; layout.setConstraints(stepsField, gbc); content.add(stepsField); gbc.gridy = 3; layout.setConstraints(rowsField, gbc); content.add(rowsField); gbc.gridy = 4; layout.setConstraints(cancelButton, gbc); content.add(cancelButton); getRootPane().setDefaultButton(okButton); // actions okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ok(); } }); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { cancel(); } }); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); } private void ok() { arcDef = createArcDef(); if(arcDef != null) { close(); } } private void close() { dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); } private void cancel() { close(); } private ArcDef createArcDef() { String consolFun = (String) consolFunCombo.getSelectedItem(); double xff; try { xff = Double.parseDouble(xffField.getText()); if(xff < 0 || xff >= 1D) { throw new NumberFormatException(); } } catch(NumberFormatException nfe) { Util.error(this, "X-files factor must be a number not less than 0.0 and less than 1.0"); return null; } int steps; try { steps = Integer.parseInt(stepsField.getText()); if(steps <= 0) { throw new NumberFormatException(); } } catch (NumberFormatException nfe) { Util.error(this, "Number of steps must be a positive integer"); return null; } int rows; try { rows = Integer.parseInt(rowsField.getText()); if(rows <= 0) { throw new NumberFormatException(); } } catch (NumberFormatException nfe) { Util.error(this, "Number of rows must be a positive integer"); return null; } try { return new ArcDef(consolFun, xff, steps, rows); } catch(RrdException e) { // should not be hear ever! e.printStackTrace(); return null; } } ArcDef getArcDef() { return arcDef; } } --- NEW FILE: EditDatasourceDialog.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.jrobin.org * Project Lead: Sasa Markovic (sa...@jr...); * * (C) Copyright 2003, by Sasa Markovic. * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * This library is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */ package org.jrobin.inspector; import org.jrobin.core.DsDef; import org.jrobin.core.RrdException; import javax.swing.*; import java.awt.*; import java.awt.event.WindowEvent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; class EditDatasourceDialog extends JDialog { private static final int FIELD_SIZE = 20; private static final String TITLE_NEW = "New datasource"; private static final String TITLE_EDIT = "Edit datasource"; private JLabel nameLabel = new JLabel("Datasource name: "); private JLabel typeLabel = new JLabel("Datasource type: "); private JLabel heartbeatLabel = new JLabel("Heartbeat: "); private JLabel minLabel = new JLabel("Min value: "); private JLabel maxLabel = new JLabel("Max value: "); private JTextField nameField = new JTextField(FIELD_SIZE); private JComboBox typeCombo = new JComboBox(); private JTextField heartbeatField = new JTextField(FIELD_SIZE); private JTextField minField = new JTextField(FIELD_SIZE); private JTextField maxField = new JTextField(FIELD_SIZE); private JButton okButton = new JButton("OK"); private JButton cancelButton = new JButton("Cancel"); private DsDef dsDef; EditDatasourceDialog(Frame parent, DsDef dsDef) { super(parent, dsDef == null? TITLE_NEW: TITLE_EDIT, true); constructUI(dsDef); pack(); Util.centerOnScreen(this); setVisible(true); } private void constructUI(DsDef dsDef) { // fill controls String[] types = DsDef.DS_TYPES; for (int i = 0; i < types.length; i++) { typeCombo.addItem(types[i]); } typeCombo.setSelectedIndex(0); if(dsDef == null) { // NEW minField.setText("U"); maxField.setText("U"); } else { // EDIT nameField.setText(dsDef.getDsName()); nameField.setEnabled(false); typeCombo.setSelectedItem(dsDef.getDsType()); typeCombo.setEnabled(false); heartbeatField.setText("" + dsDef.getHeartbeat()); minField.setText("" + dsDef.getMinValue()); maxField.setText("" + dsDef.getMaxValue()); } // layout JPanel content = (JPanel) getContentPane(); GridBagLayout layout = new GridBagLayout(); content.setLayout(layout); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(3, 3, 3, 3); gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.EAST; layout.setConstraints(nameLabel, gbc); content.add(nameLabel); gbc.gridy = 1; layout.setConstraints(typeLabel, gbc); content.add(typeLabel); gbc.gridy = 2; layout.setConstraints(heartbeatLabel, gbc); content.add(heartbeatLabel); gbc.gridy = 3; layout.setConstraints(minLabel, gbc); content.add(minLabel); gbc.gridy = 4; layout.setConstraints(maxLabel, gbc); content.add(maxLabel); gbc.gridy = 5; layout.setConstraints(okButton, gbc); okButton.setPreferredSize(cancelButton.getPreferredSize()); content.add(okButton); gbc.gridx = 1; gbc.gridy = 0; gbc.anchor = GridBagConstraints.WEST; layout.setConstraints(nameField, gbc); content.add(nameField); gbc.gridy = 1; layout.setConstraints(typeCombo, gbc); content.add(typeCombo); gbc.gridy = 2; layout.setConstraints(heartbeatField, gbc); content.add(heartbeatField); gbc.gridy = 3; layout.setConstraints(minField, gbc); content.add(minField); gbc.gridy = 4; layout.setConstraints(maxField, gbc); content.add(maxField); gbc.gridy = 5; layout.setConstraints(cancelButton, gbc); content.add(cancelButton); getRootPane().setDefaultButton(okButton); // actions okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ok(); } }); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { cancel(); } }); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); } private void ok() { dsDef = createDsDef(); if(dsDef != null) { close(); } } private void close() { dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); } private void cancel() { close(); } private DsDef createDsDef() { String name = nameField.getText(); if(name == null || name.length() < 1 || name.length() > 20) { Util.error(this, "Datasource name must be a non-empty string up to 20 chars long"); return null; } String type = (String) typeCombo.getSelectedItem(); long heartbeat; try { heartbeat = Long.parseLong(heartbeatField.getText()); if(heartbeat <= 0) { throw new NumberFormatException(); } } catch(NumberFormatException nfe) { Util.error(this, "Heartbeat must be a positive integer number"); return null; } double min = Double.NaN, max = Double.NaN; try { min = Double.parseDouble(minField.getText()); } catch (NumberFormatException nfe) { // NOP, leave NaN } try { max = Double.parseDouble(maxField.getText()); } catch (NumberFormatException nfe) { // NOP, leave NaN } if(!Double.isNaN(min) && !Double.isNaN(max) && min >= max) { Util.error(this, "Min value must be less than max value"); return null; } try { return new DsDef(name, type, heartbeat, min, max); } catch(RrdException e) { // should not be hear ever! e.printStackTrace(); return null; } } DsDef getDsDef() { return dsDef; } } --- NEW FILE: Util.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.jrobin.org * Project Lead: Sasa Markovic (sa...@jr...); * * (C) Copyright 2003, by Sasa Markovic. * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * This library is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */ package org.jrobin.inspector; import javax.swing.*; import java.awt.*; class Util { static void centerOnScreen(Window window) { Toolkit t = Toolkit.getDefaultToolkit(); Dimension screenSize = t.getScreenSize(); Dimension frameSize = window.getPreferredSize(); double x = (screenSize.getWidth() - frameSize.getWidth()) / 2; double y = (screenSize.getHeight() - frameSize.getHeight()) / 2; window.setLocation((int) x, (int) y); } static void error(Component parent, String message) { JOptionPane.showMessageDialog(parent, message, "Error", JOptionPane.ERROR_MESSAGE); } } |