PageRenderTime 84ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/build/distribution/IzPack/src/lib/com/izforge/izpack/installer/IzPanel.java

https://bitbucket.org/jorgenio/gvsig
Java | 575 lines | 269 code | 57 blank | 249 comment | 29 complexity | 4da4d46fa6a72171947dc761e6b2e820 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /*
  2. * $Id: IzPanel.java 5819 2006-06-14 07:29:09Z cesar $
  3. * IzPack
  4. * Copyright (C) 2001-2004 Julien Ponge
  5. *
  6. * File : IzPanel.java
  7. * Description : The class for the panels.
  8. * Author's email : julien@izforge.com
  9. * Author's Website : http://www.izforge.com
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. */
  25. package com.izforge.izpack.installer;
  26. import java.awt.Component;
  27. import java.awt.Font;
  28. import java.awt.GridBagConstraints;
  29. import java.awt.GridBagLayout;
  30. import java.awt.Insets;
  31. import javax.swing.ImageIcon;
  32. import javax.swing.JComponent;
  33. import javax.swing.JLabel;
  34. import javax.swing.JOptionPane;
  35. import javax.swing.JPanel;
  36. import javax.swing.LookAndFeel;
  37. import javax.swing.UIManager;
  38. import javax.swing.plaf.metal.MetalLookAndFeel;
  39. import net.n3.nanoxml.XMLElement;
  40. import com.izforge.izpack.gui.LabelFactory;
  41. import com.izforge.izpack.util.AbstractUIHandler;
  42. import com.izforge.izpack.util.MultiLineLabel;
  43. /**
  44. * Defines the base class for the IzPack panels. Any panel should be a subclass
  45. * of it and should belong to the <code>com.izforge.izpack.panels</code>
  46. * package.
  47. *
  48. * @author Julien Ponge
  49. */
  50. public class IzPanel extends JPanel implements AbstractUIHandler
  51. {
  52. /** Indicates whether grid bag layout was started or not */
  53. protected boolean gridBagLayoutStarted = false;
  54. /** The component which should get the focus at activation */
  55. protected Component initialFocus = null;
  56. /**
  57. * The installer internal data (actually a melting-pot class with all-public
  58. * fields.
  59. */
  60. protected InstallData idata;
  61. /** The parent IzPack installer frame. */
  62. protected InstallerFrame parent;
  63. /** The default grid bag constraint. */
  64. protected GridBagConstraints defaultGridBagConstraints = new GridBagConstraints ();
  65. /** Current x position of grid. */
  66. protected int gridxCounter = -1;
  67. /** Current y position of grid. */
  68. protected int gridyCounter = -1;
  69. /**
  70. * The constructor.
  71. *
  72. * @param parent The parent IzPack installer frame.
  73. * @param idata The installer internal data.
  74. */
  75. public IzPanel(InstallerFrame parent, InstallData idata)
  76. {
  77. super();
  78. this.idata = idata;
  79. this.parent = parent;
  80. }
  81. /**
  82. * Indicates wether the panel has been validated or not. The installer won't
  83. * let the user go further through the installation process until the panel
  84. * is validated. Default behaviour is to return <code>true</code>.
  85. *
  86. * @return A boolean stating wether the panel has been validated or not.
  87. */
  88. public boolean isValidated()
  89. {
  90. return true;
  91. }
  92. /**
  93. * This method is called when the panel becomes active. Default is to do
  94. * nothing : feel free to implement what you need in your subclasses. A panel
  95. * becomes active when the user reaches it during the installation process.
  96. */
  97. public void panelActivate()
  98. {
  99. }
  100. /**
  101. * This method is called when the panel gets desactivated, when the user
  102. * switches to the next panel. By default it doesn't do anything.
  103. */
  104. public void panelDeactivate()
  105. {
  106. }
  107. /**
  108. * Asks the panel to set its own XML data that can be brought back for an
  109. * automated installation process. Use it as a blackbox if your panel needs
  110. * to do something even in automated mode.
  111. *
  112. * @param panelRoot The XML root element of the panels blackbox tree.
  113. */
  114. public void makeXMLData(XMLElement panelRoot)
  115. {
  116. }
  117. /**
  118. * Ask the user a question.
  119. *
  120. * @param title Message title.
  121. * @param question The question.
  122. * @param choices The set of choices to present.
  123. *
  124. * @return The user's choice.
  125. *
  126. * @see AbstractUIHandler#askQuestion(String, String, int)
  127. */
  128. public int askQuestion(String title, String question, int choices)
  129. {
  130. return askQuestion(title, question, choices, -1);
  131. }
  132. /**
  133. * Ask the user a question.
  134. *
  135. * @param title Message title.
  136. * @param question The question.
  137. * @param choices The set of choices to present.
  138. * @param default_choice The default choice. (-1 = no default choice)
  139. *
  140. * @return The user's choice.
  141. * @see AbstractUIHandler#askQuestion(String, String, int, int)
  142. */
  143. public int askQuestion(
  144. String title,
  145. String question,
  146. int choices,
  147. int default_choice)
  148. {
  149. int jo_choices = 0;
  150. if (choices == AbstractUIHandler.CHOICES_YES_NO)
  151. jo_choices = JOptionPane.YES_NO_OPTION;
  152. else if (choices == AbstractUIHandler.CHOICES_YES_NO_CANCEL)
  153. jo_choices = JOptionPane.YES_NO_CANCEL_OPTION;
  154. int user_choice =
  155. JOptionPane.showConfirmDialog(
  156. this,
  157. (Object) question,
  158. title,
  159. jo_choices,
  160. JOptionPane.QUESTION_MESSAGE);
  161. if (user_choice == JOptionPane.CANCEL_OPTION)
  162. return AbstractUIHandler.ANSWER_CANCEL;
  163. if (user_choice == JOptionPane.YES_OPTION)
  164. return AbstractUIHandler.ANSWER_YES;
  165. if (user_choice == JOptionPane.NO_OPTION)
  166. return AbstractUIHandler.ANSWER_NO;
  167. return default_choice;
  168. }
  169. /**
  170. * Notify the user about something.
  171. *
  172. * @param message The notification.
  173. */
  174. public void emitNotification(String message)
  175. {
  176. JOptionPane.showMessageDialog(
  177. this,message);
  178. }
  179. /**
  180. * Warn the user about something.
  181. *
  182. * @param message The warning message.
  183. */
  184. public boolean emitWarning(String title, String message)
  185. {
  186. return (
  187. JOptionPane.showConfirmDialog(
  188. this,
  189. message,
  190. title,
  191. JOptionPane.WARNING_MESSAGE,
  192. JOptionPane.OK_CANCEL_OPTION)
  193. == JOptionPane.OK_OPTION);
  194. }
  195. /**
  196. * Notify the user of some error.
  197. *
  198. * @param message The error message.
  199. */
  200. public void emitError(String title, String message)
  201. {
  202. JOptionPane.showMessageDialog(
  203. this,
  204. message,
  205. title,
  206. JOptionPane.ERROR_MESSAGE);
  207. }
  208. /**
  209. * Returns the component which should be get the
  210. * focus at activation of this panel.
  211. * @return the component which should be get the
  212. * focus at activation of this panel
  213. */
  214. public Component getInitialFocus()
  215. {
  216. return initialFocus;
  217. }
  218. /**
  219. * Sets the component which should be get the
  220. * focus at activation of this panel.
  221. * @param component which should be get the
  222. * focus at activation of this panel
  223. */
  224. public void setInitialFocus(Component component)
  225. {
  226. initialFocus = component;
  227. }
  228. /**
  229. * Calls the langpack of parent InstallerFrame for the String
  230. * <tt>RuntimeClassName.subkey</tt>. Do not add a point infront
  231. * of subkey, it is always added in this method.
  232. * @param subkey the subkey for the string which should be returned
  233. * @param alternateClass the short name of the class which should be used
  234. * if no string is present with the runtime class name
  235. * @return the founded string
  236. */
  237. public String getI18nStringForClass(String subkey, String alternateClass )
  238. {
  239. String curClassName = this.getClass().getName();
  240. int nameStart = curClassName.lastIndexOf ('.') + 1;
  241. curClassName = curClassName.substring (nameStart, curClassName.length ());
  242. StringBuffer buf = new StringBuffer();
  243. buf.append(curClassName).append(".").append(subkey);
  244. String fullkey = buf.toString();
  245. String retval = parent.langpack.getString( fullkey);
  246. if( retval == null || retval.equals(fullkey) )
  247. {
  248. buf.delete(0, buf.length());
  249. buf.append(alternateClass).append(".").append(subkey);
  250. retval = parent.langpack.getString( buf.toString());
  251. }
  252. return( retval);
  253. }
  254. /**
  255. * Returns the parent of this IzPanel (which is a InstallerFrame).
  256. * @return the parent of this IzPanel
  257. */
  258. public InstallerFrame getInstallerFrame()
  259. {
  260. return(parent);
  261. }
  262. //------------- Helper for common used components ----- START ---
  263. /**
  264. * Creates a label via LabelFactory with the given ids and
  265. * the given horizontal alignment. If the icon id is null, the
  266. * label will be created also. The strings are the ids for the text
  267. * in langpack and the icon in icons of the installer frame.
  268. * @param textId id string for the text
  269. * @param iconId id string for the icon
  270. * @param pos horizontal alignment
  271. * @return the newly created label
  272. */
  273. public JLabel createLabel(String textId, String iconId, int pos)
  274. {
  275. ImageIcon ii = ( iconId != null ) ? parent.icons.getImageIcon(iconId) : null;
  276. JLabel label = LabelFactory.create(
  277. parent.langpack.getString(textId),
  278. ii,
  279. pos);
  280. if(label != null)
  281. label.setFont(getControlTextFont() );
  282. return(label);
  283. }
  284. /**
  285. * Creates a multi line label with the language dependent text
  286. * given by the text id. The strings is the id for the text
  287. * in langpack of the installer frame. The horizontal alignment
  288. * will be LEFT.
  289. * @param textId id string for the text
  290. * @return the newly created multi line label
  291. */
  292. public MultiLineLabel createMultiLineLabelLang(String textId)
  293. {
  294. return( createMultiLineLabel(
  295. parent.langpack.getString( textId) ));
  296. }
  297. /**
  298. * Creates a multi line label with the given text.
  299. * The horizontal alignment will be LEFT.
  300. * @param text text to be used in the label
  301. * @return the newly created multi line label
  302. */
  303. public MultiLineLabel createMultiLineLabel(String text)
  304. {
  305. return( createMultiLineLabel(
  306. text, null , JLabel.LEFT ));
  307. }
  308. /**
  309. * Creates a label via LabelFactory with the given text, the
  310. * given icon id and the given horizontal alignment.
  311. * If the icon id is null, the
  312. * label will be created also. The strings are the ids for the text
  313. * in langpack and the icon in icons of the installer frame.
  314. * @param text text to be used in the label
  315. * @param iconId id string for the icon
  316. * @param pos horizontal alignment
  317. * @return
  318. */
  319. public MultiLineLabel createMultiLineLabel(String text,
  320. String iconId, int pos)
  321. {
  322. MultiLineLabel mll = null;
  323. mll = new MultiLineLabel( text, 0, 0);
  324. if( mll != null)
  325. mll.setFont(getControlTextFont() );
  326. return( mll );
  327. }
  328. /**
  329. * The Font of Labels in many cases
  330. */
  331. public Font getControlTextFont()
  332. {
  333. return( getLAF() != null ? MetalLookAndFeel.getControlTextFont() : getFont() );
  334. }
  335. protected static MetalLookAndFeel getLAF()
  336. {
  337. LookAndFeel laf = UIManager.getLookAndFeel();
  338. if( laf instanceof MetalLookAndFeel)
  339. return( (MetalLookAndFeel) laf);
  340. return( null );
  341. }
  342. //------------- Helper for common used components ----- END ---
  343. //------------------- Layout stuff -------------------- START ---
  344. /**
  345. * Returns the default GridBagConstraints of this panel.
  346. * @return the default GridBagConstraints of this panel
  347. */
  348. public GridBagConstraints getDefaultGridBagConstraints()
  349. {
  350. startGridBagLayout();
  351. return defaultGridBagConstraints;
  352. }
  353. /**
  354. * Sets the default GridBagConstraints of this panel to the given object.
  355. * @param constraints which should be set as default for this object
  356. */
  357. public void setDefaultGridBagConstraints(GridBagConstraints constraints)
  358. {
  359. startGridBagLayout();
  360. defaultGridBagConstraints = constraints;
  361. }
  362. /**
  363. * Resets the grid counters which are used at getNextXGridBagConstraints
  364. * and getNextYGridBagConstraints.
  365. */
  366. public void resetGridCounter()
  367. {
  368. gridxCounter = -1;
  369. gridyCounter = -1;
  370. }
  371. /**
  372. * Returns a newly created GridBagConstraints with the given values
  373. * and the values from the defaultGridBagConstraints for the other parameters.
  374. * @param gridx value to be used for the new constraint
  375. * @param gridy value to be used for the new constraint
  376. * @return newly created GridBagConstraints with the given values
  377. * and the values from the defaultGridBagConstraints for the other parameters
  378. */
  379. public GridBagConstraints getNewGridBagConstraints(int gridx, int gridy)
  380. {
  381. GridBagConstraints retval = (GridBagConstraints) getDefaultGridBagConstraints().clone();
  382. retval.gridx = gridx;
  383. retval.gridy = gridy;
  384. return(retval);
  385. }
  386. /**
  387. * Returns a newly created GridBagConstraints with the given values
  388. * and the values from the defaultGridBagConstraints for the other parameters.
  389. * @param gridx value to be used for the new constraint
  390. * @param gridy value to be used for the new constraint
  391. * @param gridwidth value to be used for the new constraint
  392. * @param gridheight value to be used for the new constraint
  393. * @return newly created GridBagConstraints with the given values
  394. * and the values from the defaultGridBagConstraints for the other parameters
  395. */
  396. public GridBagConstraints getNewGridBagConstraints(int gridx, int gridy,
  397. int gridwidth, int gridheight)
  398. {
  399. GridBagConstraints retval = getNewGridBagConstraints( gridx, gridy );
  400. retval.gridwidth = gridwidth;
  401. retval.gridheight = gridheight;
  402. return(retval);
  403. }
  404. /**
  405. * Returns a newly created GridBagConstraints for the next column
  406. * of the current layout row.
  407. * @return a newly created GridBagConstraints for the next column
  408. * of the current layout row
  409. *
  410. */
  411. public GridBagConstraints getNextXGridBagConstraints()
  412. {
  413. gridxCounter++;
  414. GridBagConstraints retval = getNewGridBagConstraints(gridxCounter,gridyCounter);
  415. return(retval);
  416. }
  417. /**
  418. * Returns a newly created GridBagConstraints for the next column
  419. * of the current layout row using the given parameters.
  420. * @param gridwidth width for this constraint
  421. * @param gridheight height for this constraint
  422. * @return a newly created GridBagConstraints for the next column
  423. * of the current layout row using the given parameters
  424. */
  425. private GridBagConstraints getNextXGridBagConstraints( int gridwidth, int gridheight)
  426. {
  427. GridBagConstraints retval = getNextXGridBagConstraints();
  428. retval.gridwidth = gridwidth;
  429. retval.gridheight = gridheight;
  430. return( retval );
  431. }
  432. /**
  433. * Returns a newly created GridBagConstraints with column 0
  434. * for the next row.
  435. * @return a newly created GridBagConstraints with column 0
  436. * for the next row
  437. *
  438. */
  439. public GridBagConstraints getNextYGridBagConstraints()
  440. {
  441. gridyCounter++;
  442. gridxCounter = 0;
  443. GridBagConstraints retval = getNewGridBagConstraints(0,gridyCounter);
  444. return(retval);
  445. }
  446. /**
  447. * Returns a newly created GridBagConstraints with column 0
  448. * for the next row using the given parameters.
  449. * @param gridwidth width for this constraint
  450. * @param gridheight height for this constraint
  451. * @return a newly created GridBagConstraints with column 0
  452. * for the next row using the given parameters
  453. */
  454. public GridBagConstraints getNextYGridBagConstraints( int gridwidth, int gridheight)
  455. {
  456. startGridBagLayout();
  457. GridBagConstraints retval = getNextYGridBagConstraints();
  458. retval.gridwidth = gridwidth;
  459. retval.gridheight = gridheight;
  460. return( retval );
  461. }
  462. /**
  463. * Start layout determining. If it is needed, a dummy component
  464. * will be created as first row. This will be done,
  465. * if the IzPack variable <code>IzPanel.LayoutType</code> has
  466. * the value "BOTTOM".
  467. */
  468. public void startGridBagLayout()
  469. {
  470. if( gridBagLayoutStarted )
  471. return;
  472. gridBagLayoutStarted = true;
  473. GridBagLayout layout = new GridBagLayout();
  474. defaultGridBagConstraints.insets = new Insets( 0, 0, 20, 0);
  475. defaultGridBagConstraints.anchor = GridBagConstraints.WEST;
  476. setLayout(layout);
  477. String todo = idata.getVariable("IzPanel.LayoutType");
  478. if( todo == null ) // No command, no work.
  479. return;
  480. if( todo.equals("BOTTOM"))
  481. { // Make a header to push the rest to the bottom.
  482. Filler dummy = new Filler();
  483. GridBagConstraints gbConstraint = getNextYGridBagConstraints();
  484. gbConstraint.weighty = 1.0;
  485. gbConstraint.fill = GridBagConstraints.BOTH;
  486. gbConstraint.anchor = GridBagConstraints.WEST;
  487. this.add(dummy, gbConstraint );
  488. }
  489. // TODO: impl for layout type CENTER, ...
  490. }
  491. /**
  492. * Complete layout determining. If it is needed, a dummy component
  493. * will be created as last row. This will be done,
  494. * if the IzPack variable <code>IzPanel.LayoutType</code> has
  495. * the value "TOP".
  496. */
  497. public void completeGridBagLayout()
  498. {
  499. String todo = idata.getVariable("IzPanel.LayoutType");
  500. if( todo == null ) // No command, no work.
  501. return;
  502. if( todo.equals("TOP"))
  503. { // Make a footer to push the rest to the top.
  504. Filler dummy = new Filler();
  505. GridBagConstraints gbConstraint = getNextYGridBagConstraints();
  506. gbConstraint.weighty = 1.0;
  507. gbConstraint.fill = GridBagConstraints.BOTH;
  508. gbConstraint.anchor = GridBagConstraints.WEST;
  509. this.add(dummy, gbConstraint );
  510. }
  511. }
  512. //------------------- Layout stuff -------------------- END ---
  513. //------------------- Inner classes ------------------- START ---
  514. public static class Filler extends JComponent
  515. {
  516. }
  517. //------------------- Inner classes ------------------- END ---
  518. }