Changeset 13563


Ignore:
Timestamp:
2007-01-11T13:20:38+13:00 (15 years ago)
Author:
shaoqun
Message:

Added the option to allow the choose of the lauching browser programe

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/server/ServerSettings.java

    r13229 r13563  
    1414import org.greenstone.gsdl3.util.XMLConverter;
    1515
     16import org.apache.log4j.*;
     17
    1618public class ServerSettings extends JDialog
    1719
    1820{
    19     static final private String DEFPORT = "8080";
    20 
    21     //    static final private String WARN_LABEL = "Could not find a port number for tomcat. System gives a default port below:";
    22 
    23     //    static final private String PORT_LABEL = "Tomcat start port number:";
    24 
    25     //    static final private String RESTART_LABEL = "Restarting...";
    26 
    27     static final private String ICON_IMG = "gatherer_small.gif";
    28 
    29     //   static final private String AUTO_INFO = "Enter Digital library automatically";
    30 
    31     // static final private String BUILD_PROPERTIES="build.properties";
    32     // static final private String
    33     // WEB_GLOBAL_PROPERTIES="web/WEB-INF/classes/global.properties";
    34     static final private String CHALK_IMG = "chalk.gif";
    35 
    36     JPanel content_panel, top_panel, textfield_panel, comb_panel, mid_panel,
    37     down_panel;
    38 
    39     JLabel warn_label, port_label, server_label, servlet_label, restart_label;
    40 
    41     JButton start_button, conne_button, stop_button, save_button, exit_button;
    42 
     21
     22    static Logger logger = Logger.getLogger(org.greenstone.server.ServerControl.class.getName());
     23    static final int DEFPORT = 8080;
    4324    JCheckBox autoEnter;
    44 
    4525    JComboBox servlet_combobox;
    46 
    47     private JTextField portNumber_textfield = null;
    48 
    49     // private MsgDialog msgDialog=null;
    50 
     26    private JSpinner portNumber_spinner = null;
     27    private JTextField program_path_field = null;
     28    private JButton browse_button = null;
     29    private JRadioButton default_browser_button = null ;
     30    private JRadioButton other_browser_button = null;
    5131    private Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    5232
    53     static final private Dimension SIZE = new Dimension(370, 280);
    54 
    55     private String portNum = null;
    56 
     33    static final private Dimension SIZE = new Dimension(300, 350);
     34    static final Color bg_color = Color.white;
     35    private int portNum = DEFPORT;
    5736    private boolean autoStart = false;
    58 
    5937    private String servletDefault = null;
     38    private String browserPath = "";
     39    private boolean useDefaultBrowser = true;
    6040
    6141    private HashMap url_mappings = null;
    62     protected boolean configure_required = false;
    6342    private JDialog self;
    64     public ServerSettings() {
     43    private Server server;
     44     
     45    public ServerSettings(Server server) {
    6546    super((Frame)null, "", true);
    6647    this.self = this;
    67     this.portNum = Server.build_properties.getProperty(StaticStrings.TOMCAT_PORT_PROPERTY);
    68     String auto_start_str = Server.build_properties.getProperty(StaticStrings.AUTOSTART_PROPERTY);
     48        this.server = server;
     49
     50        try{
     51        this.portNum = Integer.parseInt(server.build_properties.getProperty(StaticStrings.TOMCAT_PORT_PROPERTY));
     52    }
     53        catch(Exception e){
     54        logger.error(e);
     55    }
     56
     57        this.browserPath = server.build_properties.getProperty(StaticStrings.BROWSER_PATH);
     58       
     59        if (this.browserPath == null || this.browserPath.equals("")){
     60        useDefaultBrowser = true;
     61            this.browserPath = "";
     62    }
     63    else{
     64        useDefaultBrowser = false;
     65    }
     66
     67    String auto_start_str = server.build_properties.getProperty(StaticStrings.AUTOSTART_PROPERTY);
    6968    if ((auto_start_str.trim()).equals("true")) {
    7069        this.autoStart = true;
     
    7372    }
    7473
    75     this.servletDefault = Server.build_properties.getProperty(StaticStrings.DEFAULT_SERVLET_PROPERTY);
     74    this.servletDefault = server.build_properties.getProperty(StaticStrings.DEFAULT_SERVLET_PROPERTY).replaceAll("/","");
    7675
    7776    setSize(SIZE);
    78     setTitle(Server.dictionary.get("ServerSettings.Title"));
    79     //setIconImage(Toolkit.getDefaultToolkit().getImage(Server.images_path + ICON_IMG));
     77    setTitle(server.dictionary.get("ServerSettings.Title"));
     78
    8079    setLocation((screen.width - SIZE.width) / 2,
    8180            (screen.height - SIZE.height) / 2);
    8281    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    8382
    84     content_panel = (JPanel) getContentPane();
    85     top_panel = new JPanel();
    86     textfield_panel = new JPanel();
    87     mid_panel = new JPanel();
    88     comb_panel = new JPanel();
    89     down_panel = new JPanel();
    90 
    91     //warn_label = new JLabel(WARN_LABEL);
    92     //warn_label.setForeground(Color.red);
    93 
    94     //  restart_label = new JLabel(Server.dictionary.get("ServerSettings.Restart"));
    95     //  restart_label.setForeground(Color.red);
    96     // restart_label.setVisible(false);
    97 
    98     port_label = new JLabel(Server.dictionary.get("ServerSettings.Tomcat_Port"));
    99 
    100     if (portNum != null) {
    101         //warn_label.setVisible(false);
    102         portNumber_textfield = new JTextField(portNum, 10);
    103     } else {
    104         //warn_label.setVisible(true);
    105         portNumber_textfield = new JTextField(DEFPORT, 10);
    106     }
    107     portNumber_textfield.setHorizontalAlignment(JTextField.LEFT);
    108 
    109     autoEnter = new JCheckBox(Server.dictionary.get("ServerSettings.Auto_Start"));
     83    JLabel port_label = new JLabel(Server.dictionary.get("ServerSettings.Tomcat_Port"));
     84
     85    portNumber_spinner = new JSpinner(new SpinnerNumberModel(portNum,1,65535,1));
     86    portNumber_spinner.setEditor(new JSpinner.NumberEditor(portNumber_spinner, "#####"));
     87
     88    autoEnter = new JCheckBox(server.dictionary.get("ServerSettings.Auto_Start"));
    11089   
    11190    if (autoStart) {
     
    11493        autoEnter.setSelected(false);
    11594    }
    116     autoEnter.setBackground(Color.white);
    117 
    118     servlet_label = new JLabel(Server.dictionary.get("ServerSettings.Servlet"));
     95    autoEnter.setBackground(bg_color);
     96
     97    JLabel servlet_label = new JLabel(server.dictionary.get("ServerSettings.Servlet"));
    11998
    12099    servlet_combobox = new JComboBox();
    121100    servlet_combobox.setMaximumRowCount(5);
    122     servlet_combobox.setBackground(Color.white);
     101    servlet_combobox.setBackground(bg_color);
    123102
    124103    File web_xml = new File(GlobalProperties.getProperty(StaticStrings.GSDL3_HOME_PROPERTY) + File.separator + "WEB-INF" + File.separator + "web.xml");
     
    126105    Document web_config = converter.getDOM(web_xml);
    127106    if (web_config == null) {
    128         System.err.println("web.xml is null! "+web_xml.getAbsolutePath());
     107        logger.error("web.xml is null! "+web_xml.getAbsolutePath());
    129108        return;
    130109    }
     
    145124        }
    146125
    147         if (pattern.equals(servletDefault)) {
     126        if (name.equals(servletDefault)) {
    148127        servlet_combobox.setSelectedItem(name);
    149128        }
    150 
    151     }
    152 
    153     save_button = new JButton(Server.dictionary.get("ServerSettings.OK"));
     129    }
     130
     131    JButton save_button = new JButton(Server.dictionary.get("ServerSettings.OK"));
    154132    save_button.addActionListener(new SaveActionListener());
    155133
    156     exit_button = new JButton(Server.dictionary.get("ServerSettings.Cancel"));
     134    JButton exit_button = new JButton(Server.dictionary.get("ServerSettings.Cancel"));
    157135    exit_button.addActionListener(new ActionListener() {
    158136        public void actionPerformed(ActionEvent ev) {
     
    161139        });
    162140
    163     top_panel.setBorder(BorderFactory.createEmptyBorder(15, 10, 0, 0));
    164     top_panel.setLayout(new BorderLayout());
    165     //top_panel.add(warn_label, BorderLayout.CENTER);
    166     // top_panel.add(restart_label,BorderLayout.CENTER);
    167     top_panel.setBackground(Color.white);
    168 
    169     textfield_panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    170     textfield_panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    171     textfield_panel.add(port_label);
    172     textfield_panel.add(portNumber_textfield);
    173     textfield_panel.setBackground(Color.white);
    174 
    175     comb_panel.setBorder(BorderFactory.createEmptyBorder(10, 0, 20, 0));
     141        JPanel  port_panel = new JPanel();   
     142        port_panel.setLayout(new FlowLayout(FlowLayout.LEFT));
     143    port_panel.add(port_label);
     144    port_panel.add(portNumber_spinner);
     145    port_panel.setBackground(bg_color);
     146
     147        JPanel comb_panel = new JPanel();
    176148    comb_panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    177149    comb_panel.add(servlet_label);
    178150    comb_panel.add(servlet_combobox);
    179     comb_panel.setBackground(Color.white);
    180 
    181     mid_panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 40, 0));
    182     mid_panel.setLayout(new GridLayout(3, 1));
    183     mid_panel.add(textfield_panel);
    184     mid_panel.add(autoEnter);
     151    comb_panel.setBackground(bg_color);
     152
     153       
     154    JPanel  check_panel = new JPanel();
     155        check_panel.setLayout(new FlowLayout(FlowLayout.LEFT));
     156        check_panel.add(autoEnter);
     157        check_panel.setBackground(bg_color);
     158
     159    JPanel mid_panel = new JPanel();
     160        mid_panel.setLayout(new GridLayout(3,1));
     161    mid_panel.add(port_panel);
     162    mid_panel.add(check_panel);
    185163    mid_panel.add(comb_panel);
    186     mid_panel.setBackground(Color.white);
    187 
    188     down_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
     164    mid_panel.setBackground(bg_color);
     165    mid_panel.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));
     166
     167
     168    JPanel browser_panel = new JPanel();
     169        browser_panel.setLayout(new GridLayout(4,1));
     170        default_browser_button = new JRadioButton(Server.dictionary.get("ServerSettings.DefaultBrowser"));
     171        other_browser_button = new JRadioButton(Server.dictionary.get("ServerSettings.OtherBrowser"));
     172        other_browser_button.addActionListener(new BrowserSelectionListener());
     173        default_browser_button.addActionListener(new BrowserSelectionListener());
     174
     175    ButtonGroup bg = new ButtonGroup();
     176        bg.add(default_browser_button);
     177        bg.add(other_browser_button);
     178        default_browser_button.setBorder(BorderFactory.createEmptyBorder(5,10,5,0));
     179        default_browser_button.setBackground(bg_color);     
     180        other_browser_button.setBorder(BorderFactory.createEmptyBorder(5,10,5,0));
     181        other_browser_button.setBackground(bg_color);
     182        JPanel browse_program_panel = new JPanel();     
     183        browse_program_panel.setLayout(new BorderLayout());
     184        program_path_field = new JTextField(this.browserPath);
     185        browse_button = new JButton(Server.dictionary.get("ServerSettings.Browse"));
     186    browse_program_panel.add(program_path_field,BorderLayout.CENTER);
     187        browse_program_panel.add(browse_button,BorderLayout.EAST);
     188        browse_program_panel.setBorder(BorderFactory.createEmptyBorder(5,10,5,0));
     189    browse_program_panel.setBackground(bg_color);
     190        browser_panel.add(new JLabel(Server.dictionary.get("ServerSettings.ChooseBrowser")));
     191        browser_panel.add(default_browser_button);
     192    browser_panel.add(other_browser_button);
     193        browser_panel.add(browse_program_panel);
     194    browser_panel.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));
     195        browser_panel.setBackground(bg_color);
     196    if (useDefaultBrowser){
     197        default_browser_button.setSelected(true);
     198        program_path_field.setEnabled(false);
     199        browse_button.setEnabled(false);
     200    }
     201    else{
     202        other_browser_button.setSelected(true);
     203    }
     204        browse_button.addActionListener(new BrowseActionListener());       
     205       
     206        JPanel down_panel = new JPanel();
     207    down_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
    189208    down_panel.add(save_button);
    190209    down_panel.add(exit_button);
    191     down_panel.setBackground(Color.white);
    192 
    193     ImageIcon chalk_icon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(Server.images_path + CHALK_IMG));
    194     content_panel.setBorder(BorderFactory.createMatteBorder(0, 40, 0, 0,
    195                                 chalk_icon));
    196     content_panel.setLayout(new BorderLayout());
    197 
    198     content_panel.add(top_panel, BorderLayout.NORTH);
    199     content_panel.add(mid_panel, BorderLayout.CENTER);
    200     content_panel.add(down_panel, BorderLayout.SOUTH);
    201     content_panel.setBackground(Color.white);
     210    down_panel.setBackground(bg_color);
     211    down_panel.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));
     212
     213        getContentPane().add(mid_panel, BorderLayout.NORTH);
     214        getContentPane().add(browser_panel, BorderLayout.CENTER);
     215    getContentPane().add(down_panel, BorderLayout.SOUTH);
     216    getContentPane().setBackground(bg_color);
    202217    setVisible(true);
    203218    }
    204219
    205 //     public boolean getAutoStatus() {
    206 
    207 //  if (autoEnter.isSelected()) {
    208 //      return true;
    209 //  } else {
    210 //      return false;
    211 //  }
    212 //     }
    213     public boolean isConfigureRequired() {
    214     return configure_required;
    215     }
    216 //     public String getValue(String file, String param) {
    217 //  ScriptReadWrite scriptReadWrite = new ScriptReadWrite();
    218 //  ArrayList fileLines = null;
    219 //  fileLines = scriptReadWrite.readInFile(file);
    220 //  String exValue = scriptReadWrite.existValue(fileLines, param);
    221 //  return exValue;
    222 //     }
    223 
    224 //     public boolean getAutoStart() {
    225 //  return autoStart;
    226 //     }
    227 
    228 //     public void setAutoStart(String g_properties, String auto_start) {
    229 //  String autoStart_ex = getValue(g_properties, auto_start);
    230 //  System.out.println("auto_start : "+autoStart_ex);
    231 //  if ((autoStart_ex.trim()).equals("true")) {
    232 //      this.autoStart = true;
    233 //  } else {
    234 //      this.autoStart = false;
    235 //  }
    236 //     }
    237 
    238 //     public String getPortNum() {
    239 //  return portNum;
    240 //     }
    241 
    242 //     public void setPortNum(String g_properties, String port) {
    243 //  this.portNum = getValue(g_properties, port);
    244 //     }
    245 
    246 //     public String getServletDefault() {
    247 //  return servletDefault;
    248 //     }
    249 
    250 //     public void setServletDefault(String g_properties, String servletDef) {
    251 //  this.servletDefault = getValue(g_properties, servletDef);
    252 //     }
     220    private class BrowseActionListener
     221    implements ActionListener {
     222   
     223    public void actionPerformed(ActionEvent ev) {
     224        JFileChooser chooser = new JFileChooser();
     225           int returnVal = chooser.showOpenDialog(self);       
     226       
     227        if(returnVal == JFileChooser.APPROVE_OPTION) {
     228                File program_path = chooser.getSelectedFile();
     229        program_path_field.setText(program_path.getPath());
     230        }
     231    }
     232
     233     }
     234
     235    private class BrowserSelectionListener
     236    implements ActionListener {
     237   
     238    public void actionPerformed(ActionEvent ev) {
     239        if (default_browser_button.isSelected()){
     240        program_path_field.setEnabled(false);
     241                browse_button.setEnabled(false);
     242        }
     243            else{
     244        program_path_field.setEnabled(true);
     245                browse_button.setEnabled(true);
     246        }
     247    }
     248
     249     }
     250   
    253251
    254252    private class SaveActionListener
     
    257255    public void actionPerformed(ActionEvent ev) {
    258256        // save everything to build_properties if things have changed
    259         System.out.println("auto_start: " + autoStart);
    260         System.out.println("auto_selected: " + autoEnter.isSelected());
    261         boolean has_changed = false;
    262         if (!portNum.equals(portNumber_textfield.getText().trim())) {
     257         boolean has_changed = false;
     258             boolean require_restart = false;
     259        if (portNum != ((Integer)portNumber_spinner.getValue()).intValue()) {
    263260        has_changed = true;
    264         configure_required = true;
    265         System.err.println("port changed");
     261                require_restart = true;
     262                server.reconfigRequired();
     263                logger.info("port changed, new port is "+portNumber_spinner.getValue());
    266264        }
    267265        if (autoStart != autoEnter.isSelected()) {
     266                has_changed = true;
     267        }
     268        if (!servletDefault.equals(servlet_combobox.getSelectedItem())) {
     269            has_changed = true;
     270                require_restart = true;
     271        }
     272            //changed to use other browser
     273            if (useDefaultBrowser && other_browser_button.isSelected()){
     274        browserPath = program_path_field.getText();
    268275        has_changed = true;
    269         }
    270         if (!servletDefault.equals(servlet_combobox.getSelectedItem())) {
     276        }
     277        //the browser path has been changed
     278        if ( !useDefaultBrowser && !browserPath.equals(program_path_field.getText())){
     279        browserPath = program_path_field.getText();
     280         has_changed = true;
     281        }
     282       
     283            //changed to use the default browser
     284        if ( default_browser_button.isSelected() && !useDefaultBrowser){
     285        browserPath = "";
    271286        has_changed = true;
    272287        }
     288             
     289
    273290        if (has_changed) {
    274291        ArrayList oldFileLines = null;
     
    278295        oldFileLines = scriptReadWrite.readInFile(Server.build_properties_file);
    279296       
    280         newFileLines = scriptReadWrite.queryReplace(oldFileLines, StaticStrings.TOMCAT_PORT_PROPERTY, portNumber_textfield.getText());
     297        newFileLines = scriptReadWrite.queryReplace(oldFileLines, StaticStrings.TOMCAT_PORT_PROPERTY, portNum+"");
    281298       
    282299        String newAutoEnter = (new Boolean(autoEnter.isSelected())).toString();
     
    285302        String newServletDef = (String) servlet_combobox.getSelectedItem();
    286303        newFileLines = scriptReadWrite.queryReplace(newFileLines,StaticStrings.DEFAULT_SERVLET_PROPERTY, (String) url_mappings.get(newServletDef));
    287        
     304
     305        String osName = System.getProperty("os.name");
     306                if (osName.startsWith("Windows")){
     307            browserPath = browserPath.replaceAll("\\\\","/");
     308        }
     309                 newFileLines = scriptReadWrite.queryReplace(newFileLines,StaticStrings.BROWSER_PATH, browserPath);
     310
    288311        scriptReadWrite.writeOutFile(Server.build_properties_file,
    289312                         newFileLines);
    290         Server.reloadBuildProperties();
    291         JOptionPane.showMessageDialog(null,"The new settings will be avaliable after the server restarted","Info", JOptionPane.INFORMATION_MESSAGE);
    292         } else {
    293         JOptionPane.showMessageDialog(null,"No change has been made", "Info",JOptionPane.INFORMATION_MESSAGE);
    294         }
     313        server.reloadBuildProperties();
     314        if (require_restart){
     315            JOptionPane.showMessageDialog(null,server.dictionary.get("ServerSetting.SettingChanged"),"Info", JOptionPane.INFORMATION_MESSAGE);
     316        }
     317        }
     318
    295319        self.dispose();
    296320    }
Note: See TracChangeset for help on using the changeset viewer.