1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.setup;
25 import java.awt.BorderLayout;
26 import java.awt.CardLayout;
27 import java.awt.ComponentOrientation;
28 import java.awt.Dimension;
29 import java.awt.Insets;
30 import java.awt.event.WindowAdapter;
31 import java.io.File;
32 import javax.swing.Box;
33 import javax.swing.BoxLayout;
34 import javax.swing.Icon;
35 import javax.swing.JButton;
36 import javax.swing.JDialog;
37 import javax.swing.JLabel;
38 import javax.swing.JPanel;
39 import javax.swing.JSeparator;
40 import javax.swing.border.EmptyBorder;
41 
42 public class SetupFrame extends WindowAdapter {
43 
44     String StringPrevious;
45     String StringNext;
46     String StringCancel;
47     String StringFinish;
48     String StringHelp;
49     String StringAppTitle;
50 
51     Icon   IconStarOffice;
52 
53     public static final String ACTION_NEXT      = "ActionNext";
54     public static final String ACTION_PREVIOUS  = "ActionPrevious";
55     public static final String ACTION_CANCEL    = "ActionCancel";
56     public static final String ACTION_HELP      = "ActionHelp";
57     public static final String ACTION_DETAILS   = "ActionDetails";
58     public static final String ACTION_STOP      = "ActionStop";
59 
60     public static final int CODE_OK         = 0;
61     public static final int CODE_CANCEL     = 1;
62     public static final int CODE_ERROR      = 2;
63 
64     public static final int BUTTON_NEXT     = 1;
65     public static final int BUTTON_PREVIOUS = 2;
66     public static final int BUTTON_CANCEL   = 3;
67     public static final int BUTTON_HELP     = 4;
68 
69     private JButton     mNextButton;
70     private JButton     mPreviousButton;
71     private JButton     mCancelButton;
72     private JButton     mHelpButton;
73 
74     private JDialog     mDialog;
75 
76     private JPanel      mCardPanel;
77     private CardLayout  mCardLayout;
78 
79     private SetupActionListener mActionListener;
80     private DeckOfPanels        mDeck;
81 
SetupFrame()82     public SetupFrame() {
83 
84         StringPrevious   = ResourceManager.getString("String_Previous");
85         StringNext       = ResourceManager.getString("String_Next");
86         StringCancel     = ResourceManager.getString("String_Cancel");
87         StringFinish     = ResourceManager.getString("String_Finish");
88         StringHelp       = ResourceManager.getString("String_Help");
89 
90         InstallData data = InstallData.getInstance();
91         if ( data.isInstallationMode() ) {
92             StringAppTitle   = ResourceManager.getString("String_ApplicationTitle");
93         } else {
94             StringAppTitle   = ResourceManager.getString("String_ApplicationTitleUninstallation");
95         }
96 
97         // The setup icon has to be flexible for customization, not included into the jar file
98         File iconFile = data.getInfoRoot("images");
99         iconFile = new File(iconFile, "Setup.gif");
100         IconStarOffice   = ResourceManager.getIconFromPath(iconFile);
101 
102         mActionListener = new SetupActionListener(this);
103         mDeck           = new DeckOfPanels();
104 
105         mDialog = new JDialog();
106         mDialog.setTitle(StringAppTitle);
107         initFrame();
108     }
109 
addPanel(PanelController panel, String name)110     public void addPanel(PanelController panel, String name) {
111         mCardPanel.add(panel.getPanel(), name);
112         panel.setSetupFrame(this);
113         mDeck.addPanel(panel, name);
114     }
115 
getCurrentPanel()116     public PanelController getCurrentPanel() {
117         return mDeck.getCurrentPanel();
118     }
119 
setCurrentPanel(String name, boolean ignoreRepeat, boolean isNext)120     public void setCurrentPanel(String name, boolean ignoreRepeat, boolean isNext) {
121         if (name == null)
122             close(CODE_ERROR);
123 
124         PanelController panel = mDeck.getCurrentPanel();
125         boolean repeatDialog = false;
126         if (panel != null) {
127             repeatDialog = panel.afterShow(isNext);
128             if ( isNext ) {
129                 name = panel.getNext();   // afterShow() could have changed the "next" dialog
130             }
131             if ( ignoreRepeat ) {
132                 repeatDialog = false;
133             }
134         }
135 
136         if ( repeatDialog ) {
137             name = panel.getName();
138         }
139 
140         panel = mDeck.setCurrentPanel(name);
141         if (panel != null)
142         {
143             setButtonsForPanel(panel);
144             panel.beforeShow();
145             mCardLayout.show(mCardPanel, name);
146             panel.duringShow();
147         }
148     }
149 
setButtonsForPanel(PanelController panel)150     void setButtonsForPanel(PanelController panel) {
151 
152         setButtonText(StringCancel,     BUTTON_CANCEL);
153         setButtonText(StringHelp,       BUTTON_HELP);
154         setButtonText(StringPrevious,   BUTTON_PREVIOUS);
155         // setButtonEnabled((panel.getPrevious() != null), BUTTON_PREVIOUS);
156         // setButtonEnabled((panel.getNext() != null),     BUTTON_CANCEL);
157         if (panel.getNext() == null) {
158             setButtonText(StringFinish, BUTTON_NEXT);
159         } else {
160             setButtonText(StringNext,   BUTTON_NEXT);
161         }
162     }
163 
setButtonText(String text, int button)164     public void setButtonText(String text, int button) {
165         switch (button) {
166             case BUTTON_NEXT:       mNextButton.setText(text);        break;
167             case BUTTON_PREVIOUS:   mPreviousButton.setText(text);    break;
168             case BUTTON_CANCEL:     mCancelButton.setText(text);      break;
169             case BUTTON_HELP:       mHelpButton.setText(text);        break;
170         }
171     }
172 
setButtonSelected(int button)173     public void setButtonSelected(int button) {
174         switch (button) {
175             case BUTTON_NEXT:       mNextButton.grabFocus();     break;
176             case BUTTON_PREVIOUS:   mPreviousButton.grabFocus(); break;
177             case BUTTON_CANCEL:     mCancelButton.grabFocus();   break;
178             case BUTTON_HELP:       mHelpButton.grabFocus();     break;
179         }
180     }
181 
setButtonEnabled(boolean enabled, int button)182     public void setButtonEnabled(boolean enabled, int button) {
183         switch (button) {
184             case BUTTON_NEXT:       mNextButton.setEnabled(enabled);     break;
185             case BUTTON_PREVIOUS:   mPreviousButton.setEnabled(enabled); break;
186             case BUTTON_CANCEL:     mCancelButton.setEnabled(enabled);   break;
187             case BUTTON_HELP:       mHelpButton.setEnabled(enabled);     break;
188         }
189     }
190 
removeButtonIcon(int button)191     public void removeButtonIcon(int button) {
192         switch (button) {
193             case BUTTON_NEXT:       mNextButton.setIcon(null);           break;
194             case BUTTON_PREVIOUS:   mPreviousButton.setIcon(null);       break;
195             case BUTTON_CANCEL:     mCancelButton.setIcon(null);         break;
196             case BUTTON_HELP:       mHelpButton.setIcon(null);           break;
197         }
198     }
199 
getSetupActionListener()200     public SetupActionListener getSetupActionListener() {
201         return mActionListener;
202     }
203 
close(int code)204     void close(int code) {
205         mDialog.dispose();
206     }
207 
getDialog()208     public JDialog getDialog() {
209         return mDialog;
210     }
211 
showFrame()212     public int showFrame() {
213         mDialog.pack();
214         mDialog.setLocationRelativeTo(null);
215         mDialog.setModal(true);
216         mDialog.setResizable(false);
217         // mDialog.setMinimumSize(new Dimension(679, 459));
218         mDialog.setVisible(true);
219         // System.err.println("Width: " + mDialog.getWidth() + ", Height: " + mDialog.getHeight());
220 
221         return 0;
222     }
223 
initFrame()224     private void initFrame() {
225 
226         mDialog.getContentPane().setLayout(new BorderLayout());
227 
228         mCardLayout = new CardLayout();
229         mCardPanel  = new JPanel();
230         mCardPanel.setBorder(new EmptyBorder(new Insets(5, 10, 5, 10)));
231         mCardPanel.setLayout(mCardLayout);
232 
233         mPreviousButton = new JButton();
234         mNextButton     = new JButton();
235         mCancelButton   = new JButton();
236         mHelpButton     = new JButton();
237 
238         mPreviousButton.setHorizontalTextPosition(JButton.RIGHT);
239         mNextButton.setHorizontalTextPosition(JButton.LEFT);
240 
241         mPreviousButton.setIcon(ResourceManager.getIcon("Icon_Previous"));
242         mNextButton.setIcon(ResourceManager.getIcon("Icon_Next"));
243 
244         mPreviousButton.setActionCommand(ACTION_PREVIOUS);
245         mNextButton.setActionCommand(ACTION_NEXT);
246         mCancelButton.setActionCommand(ACTION_CANCEL);
247         mHelpButton.setActionCommand(ACTION_HELP);
248 
249         mPreviousButton.addActionListener(mActionListener);
250         mNextButton.addActionListener(mActionListener);
251         mCancelButton.addActionListener(mActionListener);
252         mHelpButton.addActionListener(mActionListener);
253 
254         InstallData data = InstallData.getInstance();
255 
256         if (data.useRtl()) {
257             mPreviousButton.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
258             mNextButton.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
259             mCancelButton.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
260             mHelpButton.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
261         }
262 
263         Box ButtonBox   = new Box(BoxLayout.X_AXIS);
264         ButtonBox.setBorder(new EmptyBorder(new Insets(5, 10, 5, 10)));
265         ButtonBox.add(mPreviousButton);
266         ButtonBox.add(Box.createHorizontalStrut(10));
267         ButtonBox.add(mNextButton);
268         ButtonBox.add(Box.createHorizontalStrut(30));
269         ButtonBox.add(mCancelButton);
270         ButtonBox.add(Box.createHorizontalStrut(10));
271         ButtonBox.add(mHelpButton);
272         if (data.useRtl()) {
273             ButtonBox.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
274         }
275 
276         JPanel ButtonPanel = new JPanel();
277         JSeparator Separator = new JSeparator();
278         ButtonPanel.setLayout(new BorderLayout());
279         ButtonPanel.setPreferredSize(new Dimension(612, 44));
280         ButtonPanel.add(Separator, BorderLayout.NORTH);
281         ButtonPanel.add(ButtonBox, java.awt.BorderLayout.EAST);
282 
283         JPanel IconPanel = new JPanel();
284         JLabel Icon = new JLabel();
285         Icon.setIcon(IconStarOffice);
286 //        IconPanel.setPreferredSize(new Dimension(142, 372));
287 //        IconPanel.setBorder(new EmptyBorder(new Insets(10, 10, 10, 10)));
288         IconPanel.setLayout(new BorderLayout());
289         IconPanel.add(Icon);
290 
291         mDialog.getContentPane().add(ButtonPanel, java.awt.BorderLayout.SOUTH);
292         mDialog.getContentPane().add(mCardPanel, java.awt.BorderLayout.CENTER);
293         mDialog.getContentPane().add(IconPanel, java.awt.BorderLayout.WEST);
294     }
295 
296 }
297