/** * Popup a unit selection gui. This will also save off persistently any new unit names typed in. * * @param unit The current unit * @param defaultUnit The default unit to return if the user chooses "Default" * @return The new unit or null on a cancel or an error */ public Unit selectUnit(Unit unit, Unit defaultUnit) { JComboBox ufld = makeUnitBox(unit, defaultUnit); Component panel = GuiUtils.label(" New unit: ", ufld); if (!GuiUtils.showOkCancelDialog( null, "Change unit", GuiUtils.inset(panel, 5), null, Misc.newList(ufld))) { return null; } Object selected = ufld.getSelectedItem(); String unitName = TwoFacedObject.getIdString(selected); if (unitName == null) { return defaultUnit; } try { Unit newUnit = Util.parseUnit(unitName); if (!(selected instanceof TwoFacedObject)) { selected = new TwoFacedObject(selected.toString(), newUnit); } addToUnitList(selected); return newUnit; } catch (Exception exc) { LogUtil.userMessage("Error parsing unit:" + unitName + "\n" + exc); } return null; }
/** * Edit row * * @param paramInfo param info * @param removeOnCancel Should remove param info if user presses cancel_ * @return ok */ public boolean editRow(ParamInfo paramInfo, boolean removeOnCancel) { List comps = new ArrayList(); ParamField nameFld = new ParamField(null, true); nameFld.setText(paramInfo.getName()); JPanel topPanel = GuiUtils.hbox(GuiUtils.lLabel("Parameter: "), nameFld); topPanel = GuiUtils.inset(topPanel, 5); comps.add(GuiUtils.inset(new JLabel("Defined"), new Insets(5, 0, 0, 0))); comps.add(GuiUtils.filler()); comps.add(GuiUtils.filler()); final JLabel ctPreviewLbl = new JLabel(""); final JLabel ctLbl = new JLabel(""); if (paramInfo.hasColorTableName()) { ctLbl.setText(paramInfo.getColorTableName()); ColorTable ct = getIdv().getColorTableManager().getColorTable(paramInfo.getColorTableName()); if (ct != null) { ctPreviewLbl.setIcon(ColorTableCanvas.getIcon(ct)); } else { ctPreviewLbl.setIcon(null); } } String cbxLabel = ""; final ArrayList menus = new ArrayList(); getIdv() .getColorTableManager() .makeColorTableMenu( new ObjectListener(null) { public void actionPerformed(ActionEvent ae, Object data) { ctLbl.setText(data.toString()); ColorTable ct = getIdv().getColorTableManager().getColorTable(ctLbl.getText()); if (ct != null) { ctPreviewLbl.setIcon(ColorTableCanvas.getIcon(ct)); } else { ctPreviewLbl.setIcon(null); } } }, menus); JCheckBox ctUseCbx = new JCheckBox(cbxLabel, paramInfo.hasColorTableName()); final JButton ctPopup = new JButton("Change"); ctPopup.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { GuiUtils.showPopupMenu(menus, ctPopup); } }); addEditComponents( comps, "Color Table:", ctUseCbx, GuiUtils.hbox(ctPopup, GuiUtils.vbox(ctLbl, ctPreviewLbl), 5)); JCheckBox rangeUseCbx = new JCheckBox(cbxLabel, paramInfo.hasRange()); JTextField minFld = new JTextField("" + paramInfo.getMin(), 4); JTextField maxFld = new JTextField("" + paramInfo.getMax(), 4); JPanel rangePanel = GuiUtils.hbox(minFld, maxFld, 5); addEditComponents(comps, "Range:", rangeUseCbx, rangePanel); JCheckBox unitUseCbx = new JCheckBox(cbxLabel, paramInfo.hasDisplayUnit()); String unitLabel = ""; Unit unit = null; if (paramInfo.hasDisplayUnit()) { unit = paramInfo.getDisplayUnit(); } JComboBox unitFld = getIdv().getDisplayConventions().makeUnitBox(unit, null); // JTextField unitFld = new JTextField(unitLabel, 15); addEditComponents(comps, "Unit:", unitUseCbx, unitFld); ContourInfo ci = paramInfo.getContourInfo(); JCheckBox contourUseCbx = new JCheckBox(cbxLabel, ci != null); if (ci == null) { ci = new ContourInfo(); } ContourInfoDialog contDialog = new ContourInfoDialog("Edit Contour Defaults", false, null, false); contDialog.setState(ci); addEditComponents(comps, "Contour:", contourUseCbx, contDialog.getContents()); GuiUtils.tmpInsets = new Insets(5, 5, 5, 5); JComponent contents = GuiUtils.doLayout(comps, 3, GuiUtils.WT_NNY, GuiUtils.WT_N); contents = GuiUtils.topCenter(topPanel, contents); contents = GuiUtils.inset(contents, 5); while (true) { if (!GuiUtils.showOkCancelDialog(null, "Parameter Defaults", contents, null)) { if (removeOnCancel) { myParamInfos.remove(paramInfo); tableChanged(); } return false; } String what = ""; try { if (contourUseCbx.isSelected()) { what = "setting contour defaults"; contDialog.doApply(); ci.set(contDialog.getInfo()); paramInfo.setContourInfo(ci); } else { paramInfo.clearContourInfo(); } if (unitUseCbx.isSelected()) { what = "setting display unit"; Object selected = unitFld.getSelectedItem(); String unitName = TwoFacedObject.getIdString(selected); if ((unitName == null) || unitName.trim().equals("")) { paramInfo.setDisplayUnit(null); } else { paramInfo.setDisplayUnit(ucar.visad.Util.parseUnit(unitName)); } } else { paramInfo.setDisplayUnit(null); } if (ctUseCbx.isSelected()) { paramInfo.setColorTableName(ctLbl.getText()); } else { paramInfo.clearColorTableName(); } if (rangeUseCbx.isSelected()) { what = "setting range"; paramInfo.setRange( new Range(Misc.parseNumber(minFld.getText()), Misc.parseNumber(maxFld.getText()))); } else { paramInfo.clearRange(); } paramInfo.setName(nameFld.getText().trim()); break; } catch (Exception exc) { errorMsg("An error occurred " + what + "\n " + exc.getMessage()); // exc.printStackTrace(); } } repaint(); saveData(); return true; }
/** * get grid unit * * @return grid unit */ public String getGridUnit() { return (String) TwoFacedObject.getIdString(gridUnitCmbx.getSelectedItem()); }