/src/mpv5/ui/dialogs/Wizard.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/ · Java · 346 lines · 230 code · 44 blank · 72 comment · 12 complexity · e8cf6dc3f34e72683118695ade66ed56 MD5 · raw file

  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * Wizard.java
  19. *
  20. * Created on Dec 18, 2008, 5:14:12 AM
  21. */
  22. package mpv5.ui.dialogs;
  23. import java.awt.BorderLayout;
  24. import java.awt.Component;
  25. import java.awt.Cursor;
  26. import java.util.ArrayList;
  27. import javax.swing.JComponent;
  28. import javax.swing.JFrame;
  29. import javax.swing.JPanel;
  30. import mpv5.data.PropertyStore;
  31. import mpv5.globals.Messages;
  32. import mpv5.ui.frames.MPView;
  33. /**
  34. *
  35. *
  36. */
  37. public class Wizard extends javax.swing.JDialog implements WizardMaster {
  38. private static final long serialVersionUID = 1L;
  39. public PropertyStore actionVars = new PropertyStore();
  40. // private int index = 4300;
  41. private Component lastpanel;
  42. private boolean isEnded = false;
  43. private boolean standalone;
  44. private ArrayList<JComponent> contentlist = new ArrayList<JComponent>();
  45. ;
  46. private int level;
  47. /** Creates new form Wizard
  48. * @param standalone
  49. */
  50. public Wizard(boolean standalone) {
  51. initComponents();
  52. setModalityType(ModalityType.MODELESS);
  53. back.setEnabled(false);
  54. this.standalone = standalone;
  55. if (standalone) {
  56. this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  57. }
  58. level = 0;
  59. }
  60. public void showWiz() {
  61. back.setEnabled(false);
  62. content.add(contentlist.get(level), BorderLayout.CENTER);
  63. pack();
  64. setLocationRelativeTo(mpv5.YabsViewProxy.instance().getIdentifierFrame());
  65. setAlwaysOnTop(!standalone);
  66. setVisible(true);
  67. requestFocus();
  68. }
  69. /** This method is called from within the constructor to
  70. * initialize the form.
  71. * WARNING: Do NOT modify this code. The content of this method is
  72. * always regenerated by the Form Editor.
  73. */
  74. @SuppressWarnings("unchecked")
  75. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  76. private void initComponents() {
  77. jScrollPane1 = new javax.swing.JScrollPane();
  78. content = new javax.swing.JPanel();
  79. control = new javax.swing.JPanel();
  80. cancel = new javax.swing.JButton();
  81. back = new javax.swing.JButton();
  82. next = new javax.swing.JButton();
  83. jScrollPane2 = new javax.swing.JScrollPane();
  84. message = new javax.swing.JTextPane();
  85. progressbar = new javax.swing.JProgressBar();
  86. setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  87. java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
  88. setTitle(bundle.getString("Wizard.title")); // NOI18N
  89. setBackground(new java.awt.Color(255, 255, 255));
  90. setName("Form"); // NOI18N
  91. jScrollPane1.setName("jScrollPane1"); // NOI18N
  92. content.setBackground(new java.awt.Color(255, 255, 255));
  93. content.setBorder(javax.swing.BorderFactory.createEtchedBorder());
  94. content.setName("content"); // NOI18N
  95. content.setLayout(new java.awt.BorderLayout());
  96. jScrollPane1.setViewportView(content);
  97. getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
  98. control.setBackground(new java.awt.Color(255, 255, 255));
  99. control.setMaximumSize(new java.awt.Dimension(32767, 100));
  100. control.setName("control"); // NOI18N
  101. control.setPreferredSize(new java.awt.Dimension(375, 60));
  102. cancel.setText(bundle.getString("Wizard.cancel.text")); // NOI18N
  103. cancel.setName("cancel"); // NOI18N
  104. cancel.addActionListener(new java.awt.event.ActionListener() {
  105. public void actionPerformed(java.awt.event.ActionEvent evt) {
  106. cancelActionPerformed(evt);
  107. }
  108. });
  109. back.setText(bundle.getString("Wizard.back.text")); // NOI18N
  110. back.setName("back"); // NOI18N
  111. back.addActionListener(new java.awt.event.ActionListener() {
  112. public void actionPerformed(java.awt.event.ActionEvent evt) {
  113. backActionPerformed(evt);
  114. }
  115. });
  116. next.setText(bundle.getString("Wizard.next.text")); // NOI18N
  117. next.setName("next"); // NOI18N
  118. next.addActionListener(new java.awt.event.ActionListener() {
  119. public void actionPerformed(java.awt.event.ActionEvent evt) {
  120. nextActionPerformed(evt);
  121. }
  122. });
  123. jScrollPane2.setBorder(null);
  124. jScrollPane2.setName("jScrollPane2"); // NOI18N
  125. message.setBackground(new java.awt.Color(254, 254, 254));
  126. message.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
  127. message.setEditable(false);
  128. message.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
  129. message.setName("message"); // NOI18N
  130. jScrollPane2.setViewportView(message);
  131. progressbar.setName("progressbar"); // NOI18N
  132. javax.swing.GroupLayout controlLayout = new javax.swing.GroupLayout(control);
  133. control.setLayout(controlLayout);
  134. controlLayout.setHorizontalGroup(
  135. controlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  136. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, controlLayout.createSequentialGroup()
  137. .addContainerGap()
  138. .addComponent(cancel)
  139. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  140. .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
  141. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  142. .addComponent(back)
  143. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  144. .addComponent(next)
  145. .addContainerGap())
  146. .addComponent(progressbar, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
  147. );
  148. controlLayout.setVerticalGroup(
  149. controlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  150. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, controlLayout.createSequentialGroup()
  151. .addComponent(progressbar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  152. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  153. .addGroup(controlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  154. .addGroup(controlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  155. .addComponent(next)
  156. .addComponent(back)
  157. .addComponent(cancel))
  158. .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE))
  159. .addContainerGap())
  160. );
  161. getContentPane().add(control, java.awt.BorderLayout.SOUTH);
  162. pack();
  163. }// </editor-fold>//GEN-END:initComponents
  164. private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
  165. if (!standalone) {
  166. this.dispose();
  167. } else {
  168. System.exit(1);
  169. }
  170. }//GEN-LAST:event_cancelActionPerformed
  171. private void nextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextActionPerformed
  172. Runnable runnable = new Runnable() {
  173. public void run() {
  174. if (isEnded && standalone) {
  175. System.exit(0);
  176. } else if (isEnded) {
  177. dispose();
  178. }
  179. try {
  180. lastpanel = content.getComponent(0);
  181. if (!isEnded && ((Wizardable) lastpanel).next()) {
  182. content.remove(lastpanel);
  183. back.setEnabled(true);
  184. content.add(contentlist.get(level + 1), BorderLayout.CENTER);
  185. ((Wizardable) contentlist.get(level + 1)).load();
  186. }
  187. level++;
  188. } catch (Exception e) {
  189. next.setEnabled(false);
  190. }
  191. pack();
  192. }
  193. };
  194. new Thread(runnable).start();
  195. }//GEN-LAST:event_nextActionPerformed
  196. private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed
  197. Runnable runnable = new Runnable() {
  198. public void run() {
  199. try {
  200. if (((Wizardable) content.getComponent(0)).back() && level > 0) {
  201. content.remove(lastpanel);
  202. content.validate();
  203. validate();
  204. repaint();
  205. content.add(contentlist.get(level - 1), BorderLayout.CENTER);
  206. level--;
  207. }
  208. } catch (Exception e) {
  209. back.setEnabled(false);
  210. }
  211. pack();
  212. validate();
  213. repaint();
  214. }
  215. };
  216. new Thread(runnable).start();
  217. }//GEN-LAST:event_backActionPerformed
  218. /**
  219. * Add a apanel to this wizard. Panels will be shown in the order they are added here,
  220. * while first panel is shown first.
  221. * @param panel
  222. */
  223. public void addPanel(Wizardable panel) {
  224. JPanel pane = (JPanel) panel;
  225. // (pane).setPreferredSize(new Dimension(content.getPreferredSize()));
  226. // (pane).setBounds(0, 0, content.getWidth(), content.getHeight());
  227. // if (pane.getWidth() > content.getWidth() || pane.getHeight() > content.getHeight()) {
  228. // content.setMinimumSize(new Dimension(pane.getWidth(), pane.getHeight()));
  229. // content.setPreferredSize(new Dimension(pane.getWidth(), pane.getHeight()));
  230. // this.setMinimumSize(new Dimension(pane.getWidth() + 10, pane.getHeight() + 10));
  231. // this.setPreferredSize(new Dimension(pane.getWidth() + 10, pane.getHeight() + 10));
  232. // }
  233. (pane).setOpaque(true);
  234. contentlist.add(pane);
  235. }
  236. /**
  237. * Notifies the wizard to show the "Finnish" button which closes the wizard
  238. * @param end
  239. */
  240. public void isEnd(boolean end) {
  241. this.isEnded = end;
  242. if (end) {
  243. next.setText(Messages.FINISH.toString());
  244. }
  245. cancel.setEnabled(!end);
  246. back.setEnabled(!end);
  247. }
  248. /**
  249. * ActionVars are temporay vars which are used between different panels of this wizrd.
  250. * @return A PropertyStore which can be used to hold ActionVars
  251. */
  252. public PropertyStore getStore() {
  253. return actionVars;
  254. }
  255. /**
  256. * Set the message shown on the bottom of the wizard
  257. * @param message
  258. */
  259. public void setMessage(String message) {
  260. this.message.setText(message);
  261. }
  262. /**
  263. * @param args the command line arguments
  264. */
  265. public static void main(String args[]) {
  266. java.awt.EventQueue.invokeLater(new Runnable() {
  267. public void run() {
  268. new Wizard(true).setVisible(true);
  269. }
  270. });
  271. }
  272. // Variables declaration - do not modify//GEN-BEGIN:variables
  273. private javax.swing.JButton back;
  274. private javax.swing.JButton cancel;
  275. private javax.swing.JPanel content;
  276. private javax.swing.JPanel control;
  277. private javax.swing.JScrollPane jScrollPane1;
  278. private javax.swing.JScrollPane jScrollPane2;
  279. private javax.swing.JTextPane message;
  280. private javax.swing.JButton next;
  281. private javax.swing.JProgressBar progressbar;
  282. // End of variables declaration//GEN-END:variables
  283. /**
  284. *
  285. * @return
  286. */
  287. public Wizardable getNext() {
  288. return (Wizardable) contentlist.get(level + 1);
  289. }
  290. /**
  291. * @return the progressbar
  292. */
  293. public javax.swing.JProgressBar getProgressbar() {
  294. return progressbar;
  295. }
  296. public void enableBackButton(boolean enable) {
  297. back.setEnabled(enable);
  298. }
  299. public void setCursor(int c) {
  300. this.setCursor(Cursor.getPredefinedCursor(c));
  301. }
  302. }