Mercurial > hg > ltpda
view src/MPipeline2/src/mpipeline/plisttable/JParamValueSpecialEditor.java @ 29:54f14716c721 database-connection-manager
Update Java code
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Mon, 05 Dec 2011 16:20:06 +0100 |
parents | f0afece42f48 |
children |
line wrap: on
line source
/* * Copyright (c) 2010 Max-Planck-Gesellschaft, Ingo Diepholz <ingo.diepholz(at)aei.mpg.de> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package mpipeline.plisttable; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.EventListener; import java.util.EventObject; import java.util.Iterator; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTable; import javax.swing.event.CellEditorListener; import javax.swing.event.ChangeEvent; import javax.swing.event.EventListenerList; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableCellEditor; import javax.swing.table.TableModel; import mpipeline.canvas.MBlock; import mpipeline.dialogs.ColorListBuilder; import mpipeline.dialogs.FileInputDialog; import mpipeline.dialogs.LinestyleListBuilder; import mpipeline.dialogs.MarkerListDialog; import mpipeline.dialogs.ModelSelectionDialog; import mpipeline.dialogs.PlotinfoDialog; import mpipeline.dialogs.PzmodelEditDialog; import mpipeline.dialogs.SimpCheckBoxDialog; import mpipeline.dialogs.SimpleInputDialog; import mpipeline.dialogs.SimpleSelectionDialog; import mpipeline.dialogs.UnitsDialog; import mpipeline.geo.GEODialog; import mpipeline.jltpda.LTPDAModel; import mpipeline.main.MainWindow; import mpipeline.parametersoverview.ParametersOverviewTable; import mpipeline.parametersoverview.ParametersOverviewTableModel; /** * * @author Ingo Diepholz <ingo.diepholz(at)aei.mpg.de> */ public class JParamValueSpecialEditor implements TableCellEditor, ActionListener { /* listener list for this editor */ private EventListenerList listenerList = new EventListenerList(); private ChangeEvent changeEvent = null; /* Pointer to the MainWindow and parent window*/ private final JFrame parent; private final MainWindow mw; /* Define the "..." button for the table */ private JButton specialEdit; /* Necessary for the special editor boxes */ int currentRow = -1; int currentColumn = -1; Object currentValue = null; JTable currentTable = null; /* Command for the ActionListener */ protected static final String SPECIAL_EDIT = "SPECIAL_EDIT"; public JParamValueSpecialEditor(JFrame parent, MainWindow mw) { this.parent = parent; this.mw = mw; specialEdit = new JButton("..."); specialEdit.setActionCommand(SPECIAL_EDIT); specialEdit.addActionListener(this); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { currentTable = table; currentRow = row; // Store the column index of the paramValue and its instance. for (int i = 0; i < table.getColumnCount(); i++) { currentColumn = i; currentValue = table.getValueAt(row, i); if (currentValue instanceof JParamValue) { break; } } return specialEdit; } public Object getCellEditorValue() { return currentValue.toString(); } public boolean isCellEditable(EventObject anEvent) { if (anEvent instanceof MouseEvent) { MouseEvent mouseEvent = (MouseEvent) anEvent; if (mouseEvent.getClickCount() >= 1) { return true; } } return false; } public boolean shouldSelectCell(EventObject anEvent) { return true; } public boolean stopCellEditing() { fireEditingStopped(); return true; } public void cancelCellEditing() { fireEditingCanceled(); } public void addCellEditorListener(CellEditorListener l) { listenerList.add(CellEditorListener.class, l); } public void removeCellEditorListener(CellEditorListener l) { listenerList.remove(CellEditorListener.class, l); } protected void fireEditingStopped() { EventListener listenerListArray[] = listenerList.getListeners(CellEditorListener.class); for (int i = 0, n = listenerListArray.length; i < n; i++) { if (changeEvent == null) { changeEvent = new ChangeEvent(this); } ((CellEditorListener) listenerListArray[i]).editingStopped(changeEvent); } } protected void fireEditingCanceled() { EventListener listenerListArray[] = listenerList.getListeners(CellEditorListener.class); for (int i = 0, n = listenerListArray.length; i < n; i++) { if (changeEvent == null) { changeEvent = new ChangeEvent(this); } ((CellEditorListener) listenerListArray[i]).editingCanceled(changeEvent); } } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals(SPECIAL_EDIT)) { TableModel tableMdl = currentTable.getModel(); JPlist pl; MBlock b; String key; if (tableMdl instanceof PlistTableModel) { PlistTableModel tm = (PlistTableModel) tableMdl; pl = tm.getPl(); b = pl.getOwner(); key = pl.getParam(currentRow).getKey(); } else if (tableMdl instanceof ParametersOverviewTableModel) { ParametersOverviewTableModel tm = (ParametersOverviewTableModel) tableMdl; tm.blockAtIndex(currentRow); b = tm.blockAtIndex(currentRow); key = tm.paramAtIndex(currentRow).getKey(); pl = b.getPlist(); } else { throw new UnsupportedOperationException("Not supported Table model: " + tableMdl == null ? "NULL" : tableMdl.getClass().toString()); } // call the special editor String response = specialEditor(parent, mw, currentValue, b, pl, key); if (response != null && response != currentValue) { currentValue = response; } fireEditingStopped(); } } private String specialEditor(JFrame parent, MainWindow mw, Object currentValue, MBlock blk, JPlist pl, String key) { String response = null; // String key = pl.getParam(currentRow).getKey(); // ArrayList<Object> options = pl.getParam(currentRow).getVal().getOptions(); ArrayList<Object> options = pl.getParamForKey(key).getOptions(); if (key.toLowerCase().equals("xunit") || key.toLowerCase().equals("yunit") || key.toLowerCase().equals("yunits") || key.toLowerCase().equals("xunits") || key.toLowerCase().equals("unit") || key.toLowerCase().equals("iunits") || key.toLowerCase().equals("ounits")) { /** Special case for units key **/ UnitsDialog pd = new UnitsDialog(mw, true, "Unit Editor", "Edit " + key.toLowerCase(), currentValue.toString(), blk); pd.setVisible(true); if (!pd.isCancelled()) { response = pd.getInput(); } } else if (key.toLowerCase().equals("geoserver")) { GEODialog gd = new GEODialog(parent, true); if (!gd.isCancelled()) { response = gd.getServer(); // set other plist values pl.setValueForKey("channels", gd.getChannel()); pl.setValueForKey("starttimes", gd.getGPSLatest()); pl.setValueForKey("type", gd.getDataType()); pl.setValueForKey("port", gd.getPort()); } } else if (key.toLowerCase().equals("plotinfo")) { PlotinfoDialog pid = new PlotinfoDialog(parent, true, currentValue.toString()); pid.setVisible(true); if (!pid.isCancelled()) { response = pid.getPlistConstructor(); } } else if (key.toLowerCase().equals("linestyles")) { LinestyleListBuilder lsb = new LinestyleListBuilder(parent, true, currentValue.toString()); lsb.setVisible(true); if (!lsb.isCancelled()) { response = lsb.getLinestyleArrayString(); } } else if (key.toLowerCase().equals("markers")) { MarkerListDialog lsb = new MarkerListDialog(parent, true, currentValue.toString()); lsb.setVisible(true); if (!lsb.isCancelled()) { response = lsb.getMarkerArrayString(); } } else if (key.toLowerCase().equals("colors") || key.toLowerCase().equals("linecolors")) { ColorListBuilder clb = new ColorListBuilder(parent, true, currentValue.toString()); clb.setVisible(true); if (!clb.isCancelled()) { response = clb.getColorArray(); } } else if (key.toLowerCase().equals("filename")) { /** Special case for FILENAME key **/ // get a filename to load from the user FileInputDialog fd; if (blk.getAlgo().toLowerCase().equals("save") || blk.getAlgo().toLowerCase().equals("dotview") || blk.getAlgo().toLowerCase().equals("export")) { // if this is a save, or export, or dotview block, we want a file 'put' dialog fd = new FileInputDialog(parent, mw, true, "Save to...", currentValue.toString(), FileInputDialog.SAVE); } else { // we assume we are loading fd = new FileInputDialog(parent, mw, true, "Load from...", currentValue.toString(), FileInputDialog.LOAD); } // show dialog fd.setVisible(true); if (!fd.wasCancelled()) { response = fd.getFilename(); } } else if (key.toLowerCase().equals("waveform")) { Object[] possibilities = {"sine wave", "noise", "chirp", "gaussian pulse", "square wave", "sawtooth"}; ArrayList<String> ports = blk.getInputPortNumbersAsStrings(); ArrayList<String> choices = getChoices(possibilities, ports); String s = (String) JOptionPane.showInputDialog( null, "Waveform type", "Select Waveform Type", JOptionPane.PLAIN_MESSAGE, null, choices.toArray(), currentValue.toString()); //If a string was returned, say so. if ((s != null) && (s.length() > 0)) { response = s; // now we can set the plist based on the selected waveform if (response.equals("sine wave")) { setSineWaveParamSet(pl); } else if (response.equals("noise")) { setNoiseParamSet(pl); } else if (response.equals("chirp")) { setChirpParamSet(pl); } else if (response.equals("gaussian pulse")) { setGaussParamSet(pl); } else if (response.equals("square wave")) { setSquareParamSet(pl); } else if (response.equals("sawtooth")) { setSawParamSet(pl); } else { System.err.println("Unknown waveform"); } // Setting the changed plist to the table and block blk.setPlist(pl); fireTableChanged(pl); } } else if (key.toLowerCase().equals("type") && (blk.getMinfo().getMclass().equals("miir") || blk.getMinfo().getMclass().equals("mfir"))) { Object[] possibilities = {"lowpass", "highpass", "bandpass", "bandreject"}; ArrayList<String> ports = blk.getInputPortNumbersAsStrings(); ArrayList<String> choices = getChoices(possibilities, ports); String s = (String) JOptionPane.showInputDialog( null, "Filter type", "Select Filter Type", JOptionPane.PLAIN_MESSAGE, null, choices.toArray(), currentValue.toString()); //If a string was returned, say so. if ((s != null) && (s.length() > 0)) { response = s; } } else if (key.toLowerCase().equals("built-in")) { ArrayList<LTPDAModel> models = null; String title = null; String cl = blk.getMinfo().getMclass(); models = mw.getBuiltinModels(cl); title = "Choose a(n) " + cl + " model"; // if (blk.getMinfo().getMclass().equals("ao")) { // } else if (blk.getMinfo().getMclass().equals("ssm")) { // models = mw.getBuiltinModels("ssm"); // title = "Choose an SSM model"; // } else { // System.err.println("No built-in models for class: " + blk.getMinfo().getMclass()); // } if (models != null) { ModelSelectionDialog dg = new ModelSelectionDialog(parent, true, models); dg.setCurrentModel(currentValue.toString()); dg.setTitle(title); dg.setVisible(true); if (!dg.isCancelled()) { response = dg.getModelName(); // now get the plist // look for the model Iterator it = models.iterator(); while (it.hasNext()) { LTPDAModel mdl = (LTPDAModel) it.next(); if (mdl.getModelName().equals(response)) { // copy the model-plist and set-plist and combine them JPlist mdlpl = new JPlist(mdl.getModelPlist()); pl = new JPlist(blk.getMinfo().getPlistForSet("From Built-in Model")); pl.setValueForKey("BUILT-IN", response); pl.appendPlist(mdlpl); // Setting the changed plist to the table and block blk.setPlist(pl); fireTableChanged(pl); break; } } } } } else if (key.toLowerCase().equals("pzmodel")) { /** Special case for PZMODEL key **/ PzmodelEditDialog pd = new PzmodelEditDialog(parent, true, currentValue.toString(), blk); pd.setVisible(true); if (!pd.isCancelled()) { response = pd.getPzmodelString(); } } else if (pl.getParamForKey(key).getVal().getVal() instanceof Boolean) { SimpCheckBoxDialog cbd = new SimpCheckBoxDialog(parent, true, "asd", "Value for '" + key + "'", (Boolean) pl.getParamForKey(key).getVal().getVal()); cbd.setVisible(true); if (!cbd.isCancelled()) { response = ((Boolean) cbd.getCheckBox().isSelected()).toString(); } } else { if (options.size() == 1) { SimpleInputDialog sd = new SimpleInputDialog(parent, true, "Set Value", "Value for '" + key + "'", currentValue.toString(), blk); sd.setVisible(true); if (!sd.wasCancelled()) { response = sd.getInput(); } } else { SimpleSelectionDialog sd = new SimpleSelectionDialog(parent, true, "Set Value", "Value for '" + key + "'", currentValue, options, blk); sd.getOptionsCombo().setSelectedItem(pl.getParamForKey(key).getDefaultVal()); sd.setVisible(true); if (!sd.wasCancelled()) { response = sd.getInput(); } } } return response; } private ArrayList<String> getChoices(Object[] possibilities, ArrayList<String> ports) { ArrayList<String> choices = new ArrayList<String>(); for (int kk = 0; kk < possibilities.length; kk++) { choices.add((String) possibilities[kk]); } choices.addAll(ports); return choices; } private void setSineWaveParamSet(JPlist pl) { pl.clear(); pl.add("waveform", "sine wave", "char"); pl.add("A", 1.0, "double"); pl.add("f", 1.0, "double"); pl.add("phi", 0.0, "double"); pl.add("toff", 0.0, "double"); pl.add("fs", 1.0, "double"); pl.add("nsecs", 1.0, "double"); pl.add("t0", "1970-01-01 00:00:00.000", "char"); pl.add("Xunits", "s", "char"); pl.add("Yunits", "", "char"); } private void setNoiseParamSet(JPlist pl) { pl.clear(); pl.add("waveform", "noise", "char"); pl.add("type", "normal", "char"); pl.add("sigma", 1.0, "double"); pl.add("fs", 1.0, "double"); pl.add("nsecs", 1.0, "double"); pl.add("t0", "1970-01-01 00:00:00.000", "char"); pl.add("Xunits", "s", "char"); pl.add("Yunits", "", "char"); } private void setChirpParamSet(JPlist pl) { pl.clear(); pl.add("waveform", "chirp", "char"); pl.add("f0", 0.0, "double"); pl.add("f1", 1.0, "double"); pl.add("t1", 1.0, "double"); pl.add("fs", 1.0, "double"); pl.add("nsecs", 1.0, "double"); pl.add("t0", "1970-01-01 00:00:00.000", "char"); pl.add("Xunits", "s", "char"); pl.add("Yunits", "", "char"); } private void setGaussParamSet(JPlist pl) { pl.clear(); pl.add("waveform", "gaussian pulse", "char"); pl.add("f0", 1.0, "double"); pl.add("bw", 0.5, "double"); pl.add("fs", 1.0, "double"); pl.add("nsecs", 1.0, "double"); pl.add("t0", "1970-01-01 00:00:00.000", "char"); pl.add("Xunits", "s", "char"); pl.add("Yunits", "", "char"); } private void setSquareParamSet(JPlist pl) { pl.clear(); pl.add("waveform", "square wave", "char"); pl.add("f0", 1.0, "double"); pl.add("duty", 50, "double"); pl.add("fs", 1.0, "double"); pl.add("nsecs", 1.0, "double"); pl.add("t0", "1970-01-01 00:00:00.000", "char"); pl.add("Xunits", "s", "char"); pl.add("Yunits", "", "char"); } private void setSawParamSet(JPlist pl) { pl.clear(); pl.add("waveform", "sawtooth", "char"); pl.add("f0", 1.0, "double"); pl.add("width", 0.5, "double"); pl.add("fs", 1.0, "double"); pl.add("nsecs", 1.0, "double"); pl.add("t0", "1970-01-01 00:00:00.000", "char"); pl.add("Xunits", "s", "char"); pl.add("Yunits", "", "char"); } private void fireTableChanged(JPlist pl) { if (currentTable.getModel() instanceof PlistTableModel) { cancelCellEditing(); PlistTableModel mdl = (PlistTableModel) currentTable.getModel(); mdl.setPl(pl); } else if (currentTable.getModel() instanceof ParametersOverviewTableModel) { cancelCellEditing(); ParametersOverviewTable table = (ParametersOverviewTable) currentTable; table.reloadData(); } else { throw new UnsupportedOperationException("Not supported Table model: " + currentTable.getModel() == null ? "NULL" : currentTable.getModel().getClass().toString()); } } }