001: /*
002: * regain - A file search engine providing plenty of formats
003: * Copyright (C) 2004 Til Schneider
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: Til Schneider, info@murfman.de
020: *
021: * CVS information:
022: * $RCSfile$
023: * $Source$
024: * $Date: 2005-08-04 15:52:50 +0200 (Do, 04 Aug 2005) $
025: * $Author: til132 $
026: * $Revision: 143 $
027: */
028: package net.sf.regain.ui.desktop;
029:
030: import java.awt.BorderLayout;
031: import java.awt.Dimension;
032: import java.awt.Point;
033: import java.awt.Toolkit;
034: import java.beans.PropertyChangeEvent;
035: import java.beans.PropertyChangeListener;
036: import java.io.File;
037:
038: import javax.swing.JFileChooser;
039: import javax.swing.JFrame;
040: import javax.swing.JOptionPane;
041: import javax.swing.JPanel;
042:
043: import org.apache.log4j.Logger;
044: import org.w3c.dom.Document;
045: import org.w3c.dom.Element;
046: import org.w3c.dom.Node;
047:
048: import net.sf.regain.RegainException;
049: import net.sf.regain.XmlToolkit;
050: import net.sf.regain.util.io.Localizer;
051:
052: /**
053: * Shows a frame that asks the user to select a browser.
054: *
055: * @author Til Schneider, www.murfman.de
056: */
057: public class BrowserSelectorFrame implements DesktopConstants {
058:
059: /** The logger for this class */
060: private static Logger mLog = Logger
061: .getLogger(BrowserSelectorFrame.class);
062:
063: /** The localizer for this class. */
064: private static Localizer mLocalizer = new Localizer(
065: BrowserSelectorFrame.class);
066:
067: /** The frame. */
068: private JFrame mFrame;
069:
070: /** Option pane that contains the message. */
071: private JOptionPane mOptionPane;
072:
073: /** The page to open when a browser was chosen. */
074: private String mPageUrl;
075:
076: /**
077: * Creates a new instance of BrowserSelectorFrame.
078: *
079: * @param pageUrl The page to open when a browser was chosen.
080: */
081: public BrowserSelectorFrame(String pageUrl) {
082: mPageUrl = pageUrl;
083:
084: String msg = mLocalizer.msg("error.title", "Error");
085: mFrame = new JFrame(msg);
086:
087: JPanel main = new JPanel(new BorderLayout());
088: mFrame.setContentPane(main);
089:
090: msg = mLocalizer
091: .msg("error.message",
092: "No browser found.\n\nDo you want to choose a browser now?");
093: mOptionPane = new JOptionPane(msg, JOptionPane.ERROR_MESSAGE,
094: JOptionPane.YES_NO_OPTION);
095: mOptionPane
096: .addPropertyChangeListener(new PropertyChangeListener() {
097: public void propertyChange(PropertyChangeEvent evt) {
098: if (evt.getPropertyName().equals(
099: JOptionPane.VALUE_PROPERTY)
100: && evt.getNewValue() != null
101: && evt.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) {
102: handleOptionPaneChanged();
103: }
104: }
105: });
106: mFrame.setContentPane(mOptionPane);
107:
108: mFrame.pack();
109: }
110:
111: /**
112: * Handles a changed value. Called when yes or no was pressed.
113: */
114: void handleOptionPaneChanged() {
115: int option = ((Integer) mOptionPane.getValue()).intValue();
116:
117: if (option == 0) {
118: // Yes
119: try {
120: chooseBrowser();
121: } catch (RegainException exc) {
122: mLog.error("Choosing browser failed", exc);
123: }
124:
125: mFrame.dispose();
126: } else if (option == 1) {
127: // No
128: mFrame.dispose();
129: }
130: }
131:
132: /**
133: * Centers and shows the frame.
134: */
135: public void show() {
136: // Center the frame
137: Dimension screenSize = Toolkit.getDefaultToolkit()
138: .getScreenSize();
139: Dimension wD = mFrame.getSize();
140: Point wPos = new Point((screenSize.width - wD.width) / 2,
141: (screenSize.height - wD.height) / 2);
142: wPos.x = Math.max(0, wPos.x); // Make x > 0
143: wPos.y = Math.max(0, wPos.y); // Make y > 0
144: mFrame.setLocation(wPos);
145:
146: // Show the frame
147: mFrame.show();
148: }
149:
150: /**
151: * Chooses the browser.
152: *
153: * @throws RegainException If saving the config failed.
154: */
155: private void chooseBrowser() throws RegainException {
156: JFileChooser fileChooser = new JFileChooser();
157:
158: String msg = mLocalizer.msg("chooseBrowser", "Choose browser");
159: fileChooser.setDialogTitle(msg);
160: fileChooser.showOpenDialog(mFrame);
161:
162: File file = fileChooser.getSelectedFile();
163: if (file != null) {
164: Document desktopDoc = XmlToolkit
165: .loadXmlDocument(DESKTOP_CONFIG_FILE);
166: Element desktopConfig = desktopDoc.getDocumentElement();
167:
168: // Set the browser
169: Node browserNode = XmlToolkit.getOrAddChild(desktopDoc,
170: desktopConfig, "browser");
171: XmlToolkit.setText(desktopDoc, browserNode, file
172: .getAbsolutePath());
173:
174: // Save the config
175: XmlToolkit.saveXmlDocument(DESKTOP_CONFIG_FILE, desktopDoc);
176:
177: DesktopToolkit.openPageInBrowser(mPageUrl);
178: }
179: }
180:
181: }
|