package jsynoptic.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRootPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import simtools.ui.HeaderPanel;
import simtools.ui.MenuResourceBundle;
import simtools.ui.GridBagPanel;
import simtools.ui.ResourceFinder;
/**
* Provide an UI to configure the way to display sheets
* @author zxpletran007
*
*/
public class SheetDisplayDialog extends JDialog implements ActionListener{
public static MenuResourceBundle resources = ResourceFinder.getMenu(SheetDisplayDialog.class);
// PageSetup named properties
public static final String PRINT_VIEW = "PRINT_VIEW";
public static final String PAGE_HEIGHT = "PAGE_HEIGHT";
public static final String PAGE_WIDTH = "PAGE_WIDTH";
protected static Vector pageFormats;
static{
int paperNumber = resources.getIntValue("paperNumber");
pageFormats = new Vector(paperNumber);
for (int i=0; i<paperNumber; i++) {
PageFormat pf = new PageFormat(
resources.getIntValue("paper"+i+"Width"),
resources.getIntValue("paper"+i+"Height"),
resources.getString("paper"+i+"Name")
);
pageFormats.add(pf);
}
}
protected JRadioButton normalView;
protected JRadioButton printView;
protected JButton closeButton;
protected JButton okButton;
protected JRadioButton landscape;
protected JRadioButton portrait;
protected JComboBox paperFormatList;
protected JSpinner height;
protected JSpinner width;
protected HeaderPanel headerPanel;
JLabel lformat;
JLabel lheight;
JLabel lwidth;
boolean canApplyChanges;
public SheetDisplayDialog(Frame parent) {
super(parent, resources.getString("pageSetup"), true);
// Create contents
createContent();
canApplyChanges = false;
if (JDialog.isDefaultLookAndFeelDecorated()) {
boolean supportsWindowDecorations =
UIManager.getLookAndFeel().getSupportsWindowDecorations();
if (supportsWindowDecorations) {
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.QUESTION_DIALOG);
}
}
pack();
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
protected void createContent(){
Container content = getContentPane();
content.setLayout(new BorderLayout());
// HeaderPanel
headerPanel = new HeaderPanel(true);
headerPanel.setTitle(resources.getString("pageSetup"));
headerPanel.displayInfo(resources.getString("pageSetupTip"));
content.add(headerPanel, BorderLayout.NORTH);
// Inner panel
GridBagPanel pageSetupPanel = new GridBagPanel();
GridBagPanel pageModePanel = new GridBagPanel();
GridBagPanel pageFormatPanel = new GridBagPanel( resources.getStringValue("pageFormat"));
// Mode
normalView = resources.getRadioButton("layoutMode", this);
printView = resources.getRadioButton("pageMode", this);
normalView.setSelected(false);
printView.setSelected(false);
ButtonGroup modeGroup = new ButtonGroup();
modeGroup.add(normalView);
modeGroup.add(printView);
pageModePanel.addOnCurrentRow(normalView);
pageModePanel.carriageReturn();
pageModePanel.addOnCurrentRow(printView);
pageModePanel.carriageReturn();
// Format
paperFormatList = new JComboBox( pageFormats );
paperFormatList.addActionListener(this);
SpinnerNumberModel heightSpi = new SpinnerNumberModel();
heightSpi.setStepSize(new Integer(1));
heightSpi.setMinimum(new Integer(50));
heightSpi.setMaximum(new Integer(10000));
heightSpi.setValue(new Integer(1));
height = new JSpinner(heightSpi);
SpinnerNumberModel widthSpi = new SpinnerNumberModel();
widthSpi.setStepSize(new Integer(1));
widthSpi.setMinimum(new Integer(50));
widthSpi.setMaximum(new Integer(10000));
widthSpi.setValue(new Integer(1));
width = new JSpinner(widthSpi);
lformat = resources.getLabel("format");
pageFormatPanel.addOnCurrentRow(lformat);
pageFormatPanel.addOnCurrentRow(paperFormatList);
portrait = resources.getRadioButton("portrait", this);
landscape = resources.getRadioButton("landscape", this);
portrait.setSelected(false);
landscape.setSelected(false);
/* orientationGroup = new ButtonGroup();
orientationGroup.add(portrait);
orientationGroup.add(landscape);*/
pageFormatPanel.addOnCurrentRow(portrait);
pageFormatPanel.addOnCurrentRow(landscape);
pageFormatPanel.carriageReturn();
lwidth = resources.getLabel("width");
pageFormatPanel.addOnCurrentRow(lwidth);
pageFormatPanel.addOnCurrentRow(width);
pageFormatPanel.carriageReturn();
lheight = resources.getLabel("height");
pageFormatPanel.addOnCurrentRow(lheight);
pageFormatPanel.addOnCurrentRow(height);
pageFormatPanel.carriageReturn();
// Add all panels
pageSetupPanel.addOnCurrentRow(pageModePanel, 3, true, true, true);
pageSetupPanel.addOnCurrentRow(pageFormatPanel, 3, true, true, false);
content.add(pageSetupPanel, BorderLayout.CENTER);
// Buttons
JPanel navigationBarPanel = new JPanel(new BorderLayout());
JPanel navigationBar = new JPanel();
closeButton = resources.getButton("closeButton", this);
okButton = resources.getButton("okButton", this);
navigationBar.add(okButton);
navigationBar.add(closeButton);
navigationBarPanel.add(navigationBar, BorderLayout.EAST);
content.add(navigationBarPanel,BorderLayout.SOUTH);
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource()== normalView) {
setSheetFormatOptionsEnabled(false);
} else if (e.getSource()== printView) {
setSheetFormatOptionsEnabled(true);
} else if (e.getSource()== paperFormatList) {
updatePageSize();
} else if (e.getSource()== portrait) {
landscape.setSelected(!portrait.isSelected());
updatePageSize();
} else if (e.getSource()== landscape) {
portrait.setSelected(!landscape.isSelected());
updatePageSize();
} else if (e.getSource()==closeButton) {
dispose();
} else if (e.getSource()==okButton) {
dispose();
canApplyChanges = true;
}
}
private void updatePageSize(){
PageFormat pf = (PageFormat) paperFormatList.getSelectedItem();
if (pf != null){
int h,w;
if (portrait.isSelected()){
h = Math.max(pf.height, pf.width);
w = Math.min(pf.height, pf.width);
} else {
h = Math.min(pf.height, pf.width);
w = Math.max(pf.height, pf.width);
}
height.getModel().setValue(new Integer(h));
width.getModel().setValue( new Integer(w));
}
}
public void dispose(){
super.dispose();
canApplyChanges = false;
}
/**
* @return true if OK button has been selected
*/
public boolean canApplyChanges(){
return canApplyChanges;
}
private void setSheetFormatOptionsEnabled(boolean enable){
lformat.setEnabled(enable);
lheight.setEnabled(enable);
lwidth.setEnabled(enable);
landscape.setEnabled(enable);
portrait.setEnabled(enable);
height.setEnabled(enable);
width.setEnabled(enable);
paperFormatList.setEnabled(enable);
}
/**
* Get the value of one property given its name
* @param name the name of the property to get
* @return the property value
*/
public Object getPropertyValue(String name) {
Object res = null;
if (name.equalsIgnoreCase(PRINT_VIEW)) {
res= new Boolean (printView.isSelected());
} else if (name.equalsIgnoreCase(PAGE_HEIGHT)) {
return height.getModel().getValue();
} else if (name.equalsIgnoreCase(PAGE_WIDTH)) {
return width.getModel().getValue();
}
return res;
}
/**
* Set the value of one property given its name
* @param name the name of the property to get
* @param value its new value
*/
public void setPropertyValue(String name, Object value){
if (name.equalsIgnoreCase(PRINT_VIEW)) {
printView.setSelected( ((Boolean)value).booleanValue());
normalView.setSelected( !((Boolean)value).booleanValue());
setSheetFormatOptionsEnabled(printView.isSelected());
} else if (name.equalsIgnoreCase(PAGE_HEIGHT)) {
height.getModel().setValue(value);
int h = ((Integer)height.getModel().getValue()).intValue();
int w = ((Integer)width.getModel().getValue()).intValue();
portrait.setSelected(h >= w);
landscape.setSelected(!portrait.isSelected());
paperFormatList.setSelectedIndex(getPaperFormatIndex(h, w));
} else if (name.equalsIgnoreCase(PAGE_WIDTH)) {
width.getModel().setValue(value);
int h = ((Integer)height.getModel().getValue()).intValue();
int w = ((Integer)width.getModel().getValue()).intValue();
portrait.setSelected(h >= w);
landscape.setSelected(!portrait.isSelected());
paperFormatList.setSelectedIndex(getPaperFormatIndex(h, w));
}
}
/**
* Find the pageFormat corresponding to the given page dimensions
* @param height
* @param width
* @return the pageFormat index in the pageFormats vector, or -1 if no paper found
*/
protected int getPaperFormatIndex(int height, int width){
for(int i= 0; i< pageFormats.size(); i++){
PageFormat pf = (PageFormat)pageFormats.get(i);
if ( (height == pf.height && width == pf.width) || (height == pf.width && width == pf.height)){
return i;
}
}
return -1;
}
/**
*
* Hold a page format definition
* Page dimensions are expressed in pixels
* @author zxpletran007
*
*/
protected static class PageFormat{
int width;
int height;
String pageFormatName;
public PageFormat(int width, int height, String pageFormatName) {
super();
this.width = width;
this.height = height;
this.pageFormatName = pageFormatName;
}
public String toString(){
return pageFormatName;
}
}
}