| 1 | package org.openstreetmap.josm.gui;
|
|---|
| 2 |
|
|---|
| 3 | import java.awt.BorderLayout;
|
|---|
| 4 | import java.awt.Component;
|
|---|
| 5 | import java.awt.Container;
|
|---|
| 6 | import java.awt.Dimension;
|
|---|
| 7 | import java.awt.event.ActionEvent;
|
|---|
| 8 | import java.awt.event.ActionListener;
|
|---|
| 9 | import java.awt.event.KeyEvent;
|
|---|
| 10 | import java.io.File;
|
|---|
| 11 |
|
|---|
| 12 | import javax.swing.AbstractAction;
|
|---|
| 13 | import javax.swing.BorderFactory;
|
|---|
| 14 | import javax.swing.Box;
|
|---|
| 15 | import javax.swing.DefaultListCellRenderer;
|
|---|
| 16 | import javax.swing.ImageIcon;
|
|---|
| 17 | import javax.swing.JButton;
|
|---|
| 18 | import javax.swing.JComboBox;
|
|---|
| 19 | import javax.swing.JDialog;
|
|---|
| 20 | import javax.swing.JLabel;
|
|---|
| 21 | import javax.swing.JList;
|
|---|
| 22 | import javax.swing.JOptionPane;
|
|---|
| 23 | import javax.swing.JPanel;
|
|---|
| 24 | import javax.swing.JTabbedPane;
|
|---|
| 25 | import javax.swing.ListCellRenderer;
|
|---|
| 26 | import javax.swing.UIManager;
|
|---|
| 27 | import javax.swing.UIManager.LookAndFeelInfo;
|
|---|
| 28 |
|
|---|
| 29 | import org.openstreetmap.josm.data.Preferences;
|
|---|
| 30 | import org.openstreetmap.josm.data.Preferences.PreferencesException;
|
|---|
| 31 | import org.openstreetmap.josm.data.projection.Projection;
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * The preference settings.
|
|---|
| 35 | *
|
|---|
| 36 | * @author imi
|
|---|
| 37 | */
|
|---|
| 38 | public class PreferenceDialog extends JDialog {
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * Action to take place when user pressed the ok button.
|
|---|
| 42 | */
|
|---|
| 43 | class OkAction extends AbstractAction {
|
|---|
| 44 | public OkAction() {
|
|---|
| 45 | super("Ok", new ImageIcon("images/ok.png"));
|
|---|
| 46 | putValue(MNEMONIC_KEY, KeyEvent.VK_ENTER);
|
|---|
| 47 | }
|
|---|
| 48 | public void actionPerformed(ActionEvent e) {
|
|---|
| 49 | Preferences pref = new Preferences();
|
|---|
| 50 | pref.laf = (LookAndFeelInfo)lafCombo.getSelectedItem();
|
|---|
| 51 | pref.projection = (Projection)projectionCombo.getSelectedItem();
|
|---|
| 52 | Main.pref.projection = pref.projection;
|
|---|
| 53 | try {
|
|---|
| 54 | pref.save();
|
|---|
| 55 | } catch (PreferencesException x) {
|
|---|
| 56 | x.printStackTrace();
|
|---|
| 57 | JOptionPane.showMessageDialog(PreferenceDialog.this, "Could not save preferences:\n"+x.getMessage());
|
|---|
| 58 | }
|
|---|
| 59 | if (requiresRestart)
|
|---|
| 60 | JOptionPane.showMessageDialog(PreferenceDialog.this, "You have to restart JOSM for some settings to take effect.");
|
|---|
| 61 | setVisible(false);
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Action to take place when user pressed the cancel button.
|
|---|
| 67 | */
|
|---|
| 68 | class CancelAction extends AbstractAction {
|
|---|
| 69 | public CancelAction() {
|
|---|
| 70 | super("Cancel", new ImageIcon("images/cancel.png"));
|
|---|
| 71 | putValue(MNEMONIC_KEY, KeyEvent.VK_ESCAPE);
|
|---|
| 72 | }
|
|---|
| 73 | public void actionPerformed(ActionEvent e) {
|
|---|
| 74 | setVisible(false);
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * Indicate, that the application has to be restarted for the settings to take effect.
|
|---|
| 80 | */
|
|---|
| 81 | private boolean requiresRestart = false;
|
|---|
| 82 | /**
|
|---|
| 83 | * ComboBox with all look and feels.
|
|---|
| 84 | */
|
|---|
| 85 | private JComboBox lafCombo = new JComboBox(UIManager.getInstalledLookAndFeels());
|
|---|
| 86 | /**
|
|---|
| 87 | * The tabbed pane to add tabulars to.
|
|---|
| 88 | */
|
|---|
| 89 | private JTabbedPane tabPanel = new JTabbedPane();
|
|---|
| 90 | /**
|
|---|
| 91 | * Combobox with all projections available
|
|---|
| 92 | */
|
|---|
| 93 | private JComboBox projectionCombo = new JComboBox(Preferences.allProjections);
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 | /**
|
|---|
| 97 | * Create a preference setting dialog from an preferences-file. If the file does not
|
|---|
| 98 | * exist, it will be created.
|
|---|
| 99 | * If the dialog is closed with Ok, the preferences will be stored to the preferences-
|
|---|
| 100 | * file, otherwise no change of the file happens.
|
|---|
| 101 | */
|
|---|
| 102 | public PreferenceDialog() {
|
|---|
| 103 | super(Main.main, "Preferences");
|
|---|
| 104 |
|
|---|
| 105 | Preferences pref = new Preferences();
|
|---|
| 106 | try {
|
|---|
| 107 | if (new File(Preferences.getPreferencesFile()).exists())
|
|---|
| 108 | pref.load();
|
|---|
| 109 | } catch (PreferencesException e) {
|
|---|
| 110 | JOptionPane.showMessageDialog(Main.main, "Preferences settings could not be parsed:\n"+e.getMessage());
|
|---|
| 111 | e.printStackTrace();
|
|---|
| 112 | return;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | getContentPane().setLayout(new BorderLayout());
|
|---|
| 116 | getContentPane().add(tabPanel, BorderLayout.CENTER);
|
|---|
| 117 |
|
|---|
| 118 | newTab("Display");
|
|---|
| 119 | // laf
|
|---|
| 120 | JPanel p = newPanelLine();
|
|---|
| 121 | p.add(new JLabel("Look and Feel"));
|
|---|
| 122 | final ListCellRenderer oldRenderer = lafCombo.getRenderer();
|
|---|
| 123 | lafCombo.setRenderer(new DefaultListCellRenderer(){
|
|---|
| 124 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
|---|
| 125 | return oldRenderer.getListCellRendererComponent(list, ((LookAndFeelInfo)value).getName(), index, isSelected, cellHasFocus);
|
|---|
| 126 | }});
|
|---|
| 127 | lafCombo.setSelectedItem(pref.laf);
|
|---|
| 128 | lafCombo.addActionListener(new ActionListener(){
|
|---|
| 129 | public void actionPerformed(ActionEvent e) {
|
|---|
| 130 | setRequiresRestart();
|
|---|
| 131 | }});
|
|---|
| 132 | p.add(lafCombo);
|
|---|
| 133 |
|
|---|
| 134 | newTab("Projection");
|
|---|
| 135 | p = newPanelLine();
|
|---|
| 136 | p.add(new JLabel("Projection System"));
|
|---|
| 137 | p.add(projectionCombo);
|
|---|
| 138 | for (int i = 0; i < projectionCombo.getItemCount(); ++i)
|
|---|
| 139 | if (projectionCombo.getItemAt(i).getClass().equals(pref.projection.getClass())) {
|
|---|
| 140 | projectionCombo.setSelectedIndex(i);
|
|---|
| 141 | break;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | // OK/Cancel
|
|---|
| 145 | JPanel okPanel = new JPanel();
|
|---|
| 146 | okPanel.add(new JButton(new OkAction()));
|
|---|
| 147 | okPanel.add(new JButton(new CancelAction()));
|
|---|
| 148 | getContentPane().add(okPanel, BorderLayout.SOUTH);
|
|---|
| 149 |
|
|---|
| 150 | setModal(true);
|
|---|
| 151 | pack();
|
|---|
| 152 | Dimension s = Main.main.getSize();
|
|---|
| 153 | setLocation(s.width/2-getWidth()/2, s.height/2-getHeight()/2);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /**
|
|---|
| 157 | * Start a new tab with the given name
|
|---|
| 158 | * @param tabName The name of the new tab.
|
|---|
| 159 | */
|
|---|
| 160 | private void newTab(String tabName) {
|
|---|
| 161 | Box tab = Box.createVerticalBox();
|
|---|
| 162 | tab.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
|
|---|
| 163 | tabPanel.addTab(tabName, tab);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /**
|
|---|
| 167 | * Remember, that the settings made requires a restart of the application.
|
|---|
| 168 | * Called from various actionListener - classes
|
|---|
| 169 | */
|
|---|
| 170 | protected void setRequiresRestart() {
|
|---|
| 171 | requiresRestart = true;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | private JPanel newPanelLine() {
|
|---|
| 175 | JPanel p;
|
|---|
| 176 | p = new JPanel();
|
|---|
| 177 | p.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
|
|---|
| 178 | ((Container)tabPanel.getComponent(tabPanel.getTabCount()-1)).add(p);
|
|---|
| 179 | return p;
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|