package osp.ui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Point2D; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import jsky.coords.DMS; import jsky.coords.HMS; import jsky.coords.WorldCoords; import osp.Image; import osp.Mask; import osp.MaskHistory; import osp.MaskInterface; import osp.utils.CustomJDialog; import osp.utils.PropertiesPanel; /** * This class represents the mask properties panel found under the project tree. * It displays the alpha, delta and omega values of the currently displayed mask * (if there is any) and allows the omega value to be changed manually. */ //public class OSPE_MaskPropPanel extends PropertiesPanel implements ChangeListener { public class OSPE_MaskPropPanel extends PropertiesPanel { private static final long serialVersionUID = 1L; /** Name of the panel */ private String title = " Mask Properties "; /** Number of the "alpha" field */ public static final int alphaC = 0; /** Number of the "delta" field */ public static final int deltaC = 1; /** Number of the "set" button field */ // public static final int idBt = 2; /** Number of the "omega" field (spinner) */ public static final int omegaC = 2; private String mess; private FieldOfViewInterface fov; private Image im; private MaskInterface currentMaskI; private StatusInterface mainFrame; private JComponent compAlpha, compDelta, compOmega; private JTextField textFieldAlpha, textFieldDelta; private JSpinner omegaSpin; /** * Constructs a new mask properties panel with disabled fields. * * @param p The select panel of the application. * @param fov * @param mainFrame */ public OSPE_MaskPropPanel(final FieldOfViewInterface fov, StatusInterface mainFrame2) { this.fov = fov; if (fov.getNbrImages()>0) { im=fov.getCurrentImage(); if (im.getNbrMasks()>0) currentMaskI=fov.getCurrentImage().getCurrentMask(); } this.mainFrame = mainFrame2; setTitle(title); addTextField(alphaC, "Alpha Center"); compAlpha = getJComponent(alphaC); compAlpha.setEnabled(false); textFieldAlpha=(JTextField)compAlpha; addTextField(deltaC, "Delta Center"); compDelta = getJComponent(deltaC); compDelta.setEnabled(false); textFieldDelta=(JTextField)compDelta; textFieldAlpha.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String a=textFieldAlpha.getText(); if (verifyingCoords(a)) maskCenterUpdate(); else { new CustomJDialog("The coords format is not correct ! \n (HH:mn:ss.sss)", CustomJDialog._ERROR_,CustomJDialog._CLOSE_BUTTON_,mainFrame.getVisuPanel() ); } } }); // End Action Listener on alpha field textFieldDelta.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String b= textFieldDelta.getText(); if (verifyingCoords(b)) maskCenterUpdate(); else { //System.out.println("MaskPropPan coord pas top"); new CustomJDialog("The coords format is not correct !", CustomJDialog._ERROR_,CustomJDialog._CLOSE_BUTTON_,mainFrame.getVisuPanel() ); } } }); // End Action Listener on delta field // addButton(idBt," "); // JButton setBt=(JButton) getJComponent(idBt); // setBt.addActionListener(new ActionListener() { // @Override // public void actionPerformed(ActionEvent e) { // System.out.println("M PropPan - set bt "); // //maskCenterUpdate(); // } // }); addSpinner(omegaC, "Omega"); compOmega = getJComponent(omegaC); compOmega.setEnabled(false); compOmega.setInputVerifier(new DoubleInputVerifier()); omegaSpin= (JSpinner) compOmega; omegaSpin.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e) { double newOmg=Double.parseDouble(omegaSpin.getValue().toString()); if (fov.getNbrImages()>0) { im=fov.getCurrentImage(); if ((im.getNbrMasks()>0) && (im.getSelectedMask()>-1)) { currentMaskI= fov.getCurrentMaskOfCurrentImage(); double oOmg=currentMaskI.getOmega(); double diff = Math.abs(oOmg-newOmg); if (diff > 0.00001) { currentMaskI.getHistory().addEntry("Rotate mask"); currentMaskI.getHistory().addAction("setOmega", oOmg); currentMaskI.setOmega(newOmg); currentMaskI.update(); mainFrame.getVisuPanel().updateByHand(); mainFrame.updateButtons(); mainFrame.getOspeControl().setSaveNeed(true); } } } } }); // // ActionListener maskCenterListener = new ActionListener() { // @Override // public void actionPerformed(ActionEvent e) { // //currentMask.setAlpha( ); // //currentMask.set // maskCenterUpdate(); // currentMaskI.update(); // } // }; // textFieldAlpha.addActionListener(maskCenterListener); // textFieldDelta.addActionListener(maskCenterListener); } protected boolean verifyingCoords(String b) { // TODO Auto-generated method stub String chn=b; if (chn.matches("\\d\\d+:+\\d\\d+:+\\d\\d+\\.+\\d\\d\\d")) return true; else return false; } protected void maskCenterUpdate() { // TODO Auto-generated method stub Double valAlpha, valDelta; //System.out.println("MaskPropPan - Mask update"); WorldCoords wc=new WorldCoords(textFieldAlpha.getText(),textFieldDelta.getText()); //System.out.println(" Alp "+textFieldAlpha.getText()+ " Delt "+textFieldDelta.getText()); if (fov.getNbrImages()>0) { im=fov.getCurrentImage(); if (im.getNbrMasks()>0) { currentMaskI = (Mask) fov.getCurrentMaskOfCurrentImage(); valAlpha =wc.getRaDeg(); valDelta = wc.getDecDeg(); Point2D.Double oldMaskCenterWc=new Point2D.Double(currentMaskI.getCenterDeg().getX(),currentMaskI.getCenterDeg().getY()); Point2D.Double oldMaskCenter=new Point2D.Double(currentMaskI.getCenter().getX(),currentMaskI.getCenter().getY()); MaskHistory maskHist=currentMaskI.getHistory(); maskHist.addEntry("Move mask"); maskHist.addAction("setCenter", oldMaskCenter); maskHist.addAction("setCenterDeg", oldMaskCenterWc); maskHist.addAction("setCenterWc", oldMaskCenterWc); Point2D.Double newCenter= new Point2D.Double(valAlpha,valDelta); Point2D.Double pt=mainFrame.getVisuPanel().setNewMaskCenter(newCenter); currentMaskI.setCenterWc(newCenter.getX(),newCenter.getY()); currentMaskI.setCenterDeg(newCenter); currentMaskI.setCenter(pt); currentMaskI.update(); mainFrame.getVisuPanel().updateByHand(); mainFrame.updateButtons(); mainFrame.getOspeControl().setSaveNeed(true); } } } /** * Called automatically when the spinner value is changed by entering a * value in the spinner's text field. If the value is correct, it changes * the mask angle to the specified value. */ // public void actionPerformed(ActionEvent e) { // JTextField tf = (JTextField) e.getSource(); // double value; // // if (tf == getJComponent(omegaC)) // Omega spinner // { // if (tf.getInputVerifier().verify(getJComponent(omegaC))) { // value = Double.valueOf(tf.getText()).doubleValue(); // fov.getCurrentMaskOfCurrentImage().setOmega(value); // fov.getCurrentMaskOfCurrentImage().update(); // mainFrame.setStatusBarText(Color.blue, "INFO: Mask angle value is " + value); // } else // mainFrame.setStatusBarText(Color.red, // "WARNING: Mask angle value must be a double !!!"); // } // } //TODO to remove? /** * Called automatically when the spinner value is changed by clicking on the * spinner's arrows. It changes the mask angle to the specified value. * 01/06/2015 NON ! Ne fonctionne pas. * */ // public void stateChanged(ChangeEvent arg0) { // // JSpinner tf = (JSpinner) arg0.getSource(); // double value; // // if (tf == getJComponent(omegaC)) // Omega spinner // { // if (fov.getSelectedImage() >= 0) { // value = Double.valueOf(String.valueOf(tf.getModel().getValue())).doubleValue(); // if (fov.getSelectedMaskOfCurrentImage() >= 0) { // MaskInterface currentMask = fov.getCurrentMaskOfCurrentImage(); // // Double oldOmega = new Double(currentMask.getOmega()); // if (oldOmega.doubleValue() != value && !currentMask.isTurning()) { // currentMask.addEntryToHistory("Change mask angle"); // currentMask.addActionToHistory("setOmega", oldOmega); // } // // currentMask.setOmega(value); // currentMask.update(); // // mainFrame.setStatusBarText(Color.blue, "INFO: Mask angle value is " + value); // } // } // } // } // /** * Updates the masks data (alpha, delta & omega) in the mask properties * panel. * @param isMask */ public void updateMaskProp(boolean isMask) { if (fov.getSelectedImage() >= 0 && fov.getSelectedMaskOfCurrentImage() >= 0) { MaskInterface currentMask = fov.getCurrentMaskOfCurrentImage(); setValue(OSPE_MaskPropPanel.alphaC, currentMask.getMaskCenterWordlCoord().getRA()); setValue(OSPE_MaskPropPanel.deltaC, currentMask.getMaskCenterWordlCoord().getDec()); setSpinnerValue(OSPE_MaskPropPanel.omegaC, currentMask.getOmega()); getJComponent(OSPE_MaskPropPanel.alphaC).setEnabled(true); getJComponent(OSPE_MaskPropPanel.deltaC).setEnabled(true); // getJComponent(OSPE_MaskPropPanel.idBt).setEnabled(true); getJComponent(OSPE_MaskPropPanel.omegaC).setEnabled(true); } else { setValue(OSPE_MaskPropPanel.alphaC, ""); setValue(OSPE_MaskPropPanel.deltaC, ""); setSpinnerValue(OSPE_MaskPropPanel.omegaC, 0); getJComponent(OSPE_MaskPropPanel.alphaC).setEnabled(false); getJComponent(OSPE_MaskPropPanel.deltaC).setEnabled(false); // getJComponent(OSPE_MaskPropPanel.idBt).setEnabled(false); getJComponent(OSPE_MaskPropPanel.omegaC).setEnabled(false); } } public static void main(String[] args) { OSPE_MaskPropPanel maskPropPanel = new OSPE_MaskPropPanel(new FieldOfViewInterface() { private double omega = 0.; Point2D.Double p = new Point2D.Double(0.0,0.0); @Override public int getSelectedMaskOfCurrentImage() { return 0; } @Override public int getSelectedImage() { return 0; } @Override public int getNbrImages() { return 0; } @Override public MaskInterface getCurrentMaskOfCurrentImage() { return new MaskInterface() { @Override public void update() { } public void setCenter(Point2D.Double pt) { p=pt; } public void setCenterDeg(Point2D.Double pt) { p=pt; } @Override public void setOmega(double value) { omega = value; } @Override public boolean isTurning() { return false; } @Override public MaskHistory getHistory() { return null; } @Override public double getOmega() { return omega; } @Override public void addEntryToHistory(String string) { } @Override public void addActionToHistory(String string, Double oldOmega) { } @Override public HMS getAlpha() { return new HMS(5.0); } @Override public DMS getDelta() { return new DMS(2.0); } @Override public java.awt.geom.Point2D.Double getCenter() { // TODO Auto-generated method stub return null; } @Override public java.awt.geom.Point2D.Double getCenterDeg() { // TODO Auto-generated method stub return null; } @Override public WorldCoords getMaskCenterWordlCoord() { // TODO Auto-generated method stub return null; } @Override public void setCenterWc(double ra, double dec) { // TODO Auto-generated method stub } }; } @Override public Image getCurrentImage() { // TODO Auto-generated method stub return null; } }, new StatusInterface() { @Override public void setStatusBarText(Color blue, String val) { System.out.println("Color ="+ blue + ": " + val); } @Override public OSPE_VisuPanel getVisuPanel() { return null; } @Override public OspeControl getOspeControl() { return null; } @Override public void setStatusBarText(String val) { System.out.println(val); } @Override public void updateButtons(){}; }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(maskPropPanel); frame.setVisible(true); frame.pack(); // maskPropPanel.updateMaskProp(false); } }