Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / build / distribution / IzPack / src / lib / com / izforge / izpack / installer / IzPanel.java @ 23393

History | View | Annotate | Download (17.9 KB)

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

    
27
import java.awt.Component;
28
import java.awt.Font;
29
import java.awt.GridBagConstraints;
30
import java.awt.GridBagLayout;
31
import java.awt.Insets;
32

    
33
import javax.swing.ImageIcon;
34
import javax.swing.JComponent;
35
import javax.swing.JLabel;
36
import javax.swing.JOptionPane;
37
import javax.swing.JPanel;
38
import javax.swing.LookAndFeel;
39
import javax.swing.UIManager;
40
import javax.swing.plaf.metal.MetalLookAndFeel;
41

    
42
import net.n3.nanoxml.XMLElement;
43

    
44
import com.izforge.izpack.gui.LabelFactory;
45
import com.izforge.izpack.util.AbstractUIHandler;
46
import com.izforge.izpack.util.MultiLineLabel;
47

    
48
/**
49
 *  Defines the base class for the IzPack panels. Any panel should be a subclass
50
 *  of it and should belong to the <code>com.izforge.izpack.panels</code>
51
 *  package.
52
 *
53
 * @author     Julien Ponge
54
 */
55
public class IzPanel extends JPanel implements AbstractUIHandler
56
{
57
  /** Indicates whether grid bag layout was started or not */
58
  protected boolean gridBagLayoutStarted = false;
59
  
60
  /** The component which should get the focus at activation */
61
  protected Component initialFocus = null;
62
  /**
63
   *  The installer internal data (actually a melting-pot class with all-public
64
   *  fields.
65
   */
66
  protected InstallData idata;
67

    
68
  /**  The parent IzPack installer frame. */
69
  protected InstallerFrame parent;
70

    
71
  /** The default grid bag constraint. */
72
  protected GridBagConstraints defaultGridBagConstraints = new GridBagConstraints ();
73

    
74
  /** Current x position of grid. */
75
  protected int gridxCounter = -1;
76
  
77
  /** Current y position of grid. */
78
  protected int gridyCounter = -1;
79
  
80
  /**
81
   *  The constructor.
82
   *
83
   * @param  parent  The parent IzPack installer frame.
84
   * @param  idata   The installer internal data.
85
   */
86
  public IzPanel(InstallerFrame parent, InstallData idata)
87
  {
88
    super();
89

    
90
    this.idata = idata;
91
    this.parent = parent;
92
  }
93

    
94
  /**
95
   *  Indicates wether the panel has been validated or not. The installer won't
96
   *  let the user go further through the installation process until the panel
97
   *  is validated. Default behaviour is to return <code>true</code>.
98
   *
99
   * @return    A boolean stating wether the panel has been validated or not.
100
   */
101
  public boolean isValidated()
102
  {
103
    return true;
104
  }
105

    
106
  /**
107
   *  This method is called when the panel becomes active. Default is to do
108
   *  nothing : feel free to implement what you need in your subclasses. A panel
109
   *  becomes active when the user reaches it during the installation process.
110
   */
111
  public void panelActivate()
112
  {
113
  }
114

    
115
  /**
116
   *  This method is called when the panel gets desactivated, when the user
117
   *  switches to the next panel. By default it doesn't do anything.
118
   */
119
  public void panelDeactivate()
120
  {
121
  }
122

    
123
  /**
124
   *  Asks the panel to set its own XML data that can be brought back for an
125
   *  automated installation process. Use it as a blackbox if your panel needs
126
   *  to do something even in automated mode.
127
   *
128
   * @param  panelRoot  The XML root element of the panels blackbox tree.
129
   */
130
  public void makeXMLData(XMLElement panelRoot)
131
  {
132
  }
133

    
134
  /**
135
   * Ask the user a question.
136
   * 
137
   * @param title Message title.
138
   * @param question The question.
139
   * @param choices The set of choices to present.
140
   * 
141
   * @return The user's choice.
142
   * 
143
   * @see AbstractUIHandler#askQuestion(String, String, int)
144
   */
145
  public int askQuestion(String title, String question, int choices)
146
  {
147
    return askQuestion(title, question, choices, -1);
148
  }
149

    
150
  /**
151
   * Ask the user a question.
152
   * 
153
   * @param title Message title.
154
   * @param question The question.
155
   * @param choices The set of choices to present.
156
   * @param default_choice The default choice. (-1 = no default choice)
157
   * 
158
   * @return The user's choice.
159
   * @see AbstractUIHandler#askQuestion(String, String, int, int)
160
   */
161
  public int askQuestion(
162
    String title,
163
    String question,
164
    int choices,
165
    int default_choice)
166
  {
167
    int jo_choices = 0;
168

    
169
    if (choices == AbstractUIHandler.CHOICES_YES_NO)
170
      jo_choices = JOptionPane.YES_NO_OPTION;
171
    else if (choices == AbstractUIHandler.CHOICES_YES_NO_CANCEL)
172
      jo_choices = JOptionPane.YES_NO_CANCEL_OPTION;
173

    
174
    int user_choice =
175
      JOptionPane.showConfirmDialog(
176
        this,
177
        (Object) question,
178
        title,
179
        jo_choices,
180
        JOptionPane.QUESTION_MESSAGE);
181

    
182
    if (user_choice == JOptionPane.CANCEL_OPTION)
183
      return AbstractUIHandler.ANSWER_CANCEL;
184

    
185
    if (user_choice == JOptionPane.YES_OPTION)
186
      return AbstractUIHandler.ANSWER_YES;
187

    
188
    if (user_choice == JOptionPane.NO_OPTION)
189
      return AbstractUIHandler.ANSWER_NO;
190

    
191
    return default_choice;
192
  }
193

    
194
  /**
195
   * Notify the user about something.
196
   * 
197
   * @param message The notification.
198
   */
199
  public void emitNotification(String message)
200
  {
201
     JOptionPane.showMessageDialog(
202
      this,message);
203
  }
204

    
205
  /**
206
   * Warn the user about something.
207
   * 
208
   * @param message The warning message.
209
   */
210
  public boolean emitWarning(String title, String message)
211
  {
212
    return (
213
      JOptionPane.showConfirmDialog(
214
        this,
215
        message,
216
        title,
217
        JOptionPane.WARNING_MESSAGE,
218
        JOptionPane.OK_CANCEL_OPTION)
219
        == JOptionPane.OK_OPTION);
220

    
221
  }
222

    
223
  /**
224
   * Notify the user of some error.
225
   * 
226
   * @param message The error message.
227
   */
228
  public void emitError(String title, String message)
229
  {
230
    JOptionPane.showMessageDialog(
231
      this,
232
      message,
233
      title,
234
      JOptionPane.ERROR_MESSAGE);
235
  }
236

    
237
  /**
238
   * Returns the component which should be get the
239
   * focus at activation of this panel.
240
   * @return the component which should be get the
241
   * focus at activation of this panel
242
   */
243
  public Component getInitialFocus()
244
  {
245
    return initialFocus;
246
  }
247

    
248
  /**
249
   * Sets the component which should be get the
250
   * focus at activation of this panel.
251
   * @param component which should be get the
252
   * focus at activation of this panel
253
   */
254
  public void setInitialFocus(Component component)
255
  {
256
    initialFocus = component;
257
  }
258

    
259
  /**
260
   * Calls the langpack of parent InstallerFrame for the String 
261
   * <tt>RuntimeClassName.subkey</tt>. Do not add a point infront
262
   * of subkey, it is always added in this method.
263
   * @param subkey the subkey for the string which should be returned
264
   * @param alternateClass the short name of the class which should be used 
265
   * if no string is present with the runtime class name 
266
   * @return the founded string
267
   */
268
  public String getI18nStringForClass(String subkey, String alternateClass )
269
  {
270
    String curClassName = this.getClass().getName();
271
    int     nameStart     = curClassName.lastIndexOf ('.') + 1;
272
    curClassName          = curClassName.substring (nameStart, curClassName.length ());
273
    StringBuffer buf = new StringBuffer();
274
    buf.append(curClassName).append(".").append(subkey);
275
    String fullkey = buf.toString();
276
    String retval = parent.langpack.getString( fullkey);
277
    if( retval == null || retval.equals(fullkey) )
278
    {
279
      buf.delete(0, buf.length());
280
      buf.append(alternateClass).append(".").append(subkey);
281
      retval = parent.langpack.getString( buf.toString());
282
    }
283
    return( retval);
284
  }
285

    
286
  /**
287
   * Returns the parent of this IzPanel (which is a InstallerFrame).
288
   * @return the parent of this IzPanel
289
   */
290
  public InstallerFrame getInstallerFrame()
291
  {
292
    return(parent);
293
  }
294

    
295
  //------------- Helper for common used components ----- START ---
296

    
297
  /**
298
   * Creates  a label via LabelFactory with the given ids and
299
   * the given horizontal alignment. If the icon id is null, the
300
   * label will be created also. The strings are the ids for the text
301
   * in langpack and the icon in icons of the installer frame.
302
   * @param textId id string for the text
303
   * @param iconId id string for the icon
304
   * @param pos horizontal alignment
305
   * @return the newly created label
306
   */
307
  public JLabel createLabel(String textId, String iconId, int pos)
308
  {
309
    ImageIcon ii = ( iconId != null ) ? parent.icons.getImageIcon(iconId) : null;
310
    JLabel label = LabelFactory.create(
311
      parent.langpack.getString(textId),
312
      ii, 
313
      pos);
314
    if(label != null)
315
      label.setFont(getControlTextFont() );
316
    return(label);
317
    
318
  }
319

    
320
  /**
321
   * Creates a multi line label with the language dependent text
322
   * given by the text id. The strings is the id for the text
323
   * in langpack of the installer frame. The horizontal alignment
324
   * will be LEFT.
325
   * @param textId id string for the text
326
   * @return the newly created multi line label
327
   */
328
  public MultiLineLabel createMultiLineLabelLang(String textId)
329
  {
330
    return( createMultiLineLabel(
331
      parent.langpack.getString( textId)  ));
332
  }
333

    
334
  /**
335
   * Creates a multi line label with the given text. 
336
   * The horizontal alignment will be LEFT.
337
   * @param text text to be used in the label
338
   * @return the newly created multi line label
339
   */
340
  public MultiLineLabel createMultiLineLabel(String text)
341
  {
342
    return( createMultiLineLabel(
343
      text, null , JLabel.LEFT ));
344
  }
345
  /**
346
   * Creates  a label via LabelFactory with the given text, the
347
   * given icon id  and the given horizontal alignment. 
348
   * If the icon id is null, the
349
   * label will be created also. The strings are the ids for the text
350
   * in langpack and the icon in icons of the installer frame.
351
   * @param text text to be used in the label
352
   * @param iconId id string for the icon
353
   * @param pos horizontal alignment
354
   * @return
355
   */
356
  public MultiLineLabel createMultiLineLabel(String text, 
357
    String iconId, int pos)
358
  {
359
    MultiLineLabel mll = null;
360
    mll = new MultiLineLabel( text, 0, 0);
361
    if( mll != null)
362
      mll.setFont(getControlTextFont() );
363
    return( mll );
364
  }
365

    
366
  /**
367
   * The Font of Labels in many cases
368
   */
369
  public Font getControlTextFont()
370
  {
371
    return( getLAF() != null ? MetalLookAndFeel.getControlTextFont() : getFont() );
372
  }
373

    
374
  protected static MetalLookAndFeel getLAF()
375
  {
376
    LookAndFeel laf = UIManager.getLookAndFeel();
377
    if( laf instanceof MetalLookAndFeel)
378
      return( (MetalLookAndFeel) laf);
379
    return( null );
380
  }
381

    
382
  //------------- Helper for common used components ----- END ---
383
  //------------------- Layout stuff -------------------- START ---
384
  /**
385
   * Returns the default GridBagConstraints of this panel.
386
   * @return the default GridBagConstraints of this panel
387
   */
388
  public GridBagConstraints getDefaultGridBagConstraints()
389
  {
390
    startGridBagLayout();
391
    return defaultGridBagConstraints;
392
  }
393

    
394
  /**
395
   * Sets the default GridBagConstraints of this panel to the given object.
396
   * @param constraints which should be set as default for this object
397
   */
398
  public void setDefaultGridBagConstraints(GridBagConstraints constraints)
399
  {
400
    startGridBagLayout();
401
    defaultGridBagConstraints = constraints;
402
  }
403
  /**
404
   * Resets the grid counters which are used at getNextXGridBagConstraints
405
   * and getNextYGridBagConstraints.
406
   */
407
  public void resetGridCounter()
408
  {
409
    gridxCounter = -1;
410
    gridyCounter = -1;
411
  }
412

    
413
  /**
414
   * Returns a newly created GridBagConstraints with the given values
415
   * and the values from the defaultGridBagConstraints for the other parameters.
416
   * @param gridx  value to be used for the new constraint
417
   * @param gridy  value to be used for the new constraint
418
   * @return newly created GridBagConstraints with the given values
419
   * and the values from the defaultGridBagConstraints for the other parameters
420
   */
421
  public GridBagConstraints getNewGridBagConstraints(int gridx, int gridy)
422
  {
423
    GridBagConstraints retval = (GridBagConstraints) getDefaultGridBagConstraints().clone();
424
    retval.gridx = gridx;
425
    retval.gridy = gridy;
426
    return(retval);
427
    
428
  }
429
  
430
  /**
431
   * Returns a newly created GridBagConstraints with the given values
432
   * and the values from the defaultGridBagConstraints for the other parameters.
433
   * @param gridx  value to be used for the new constraint
434
   * @param gridy  value to be used for the new constraint
435
   * @param gridwidth value to be used for the new constraint
436
   * @param gridheight value to be used for the new constraint
437
   * @return newly created GridBagConstraints with the given values
438
   * and the values from the defaultGridBagConstraints for the other parameters
439
   */
440
  public GridBagConstraints getNewGridBagConstraints(int gridx, int gridy,
441
    int gridwidth, int gridheight)
442
  {
443
    GridBagConstraints retval = getNewGridBagConstraints( gridx, gridy );
444
    retval.gridwidth = gridwidth;
445
    retval.gridheight = gridheight;
446
    return(retval);
447
  }
448
  
449
  /**
450
   * Returns a newly created GridBagConstraints for the next column 
451
   * of the current layout row.
452
   * @return a newly created GridBagConstraints for the next column 
453
   * of the current layout row
454
   * 
455
   */
456
  public GridBagConstraints getNextXGridBagConstraints()
457
  {
458
    gridxCounter++;
459
    GridBagConstraints retval = getNewGridBagConstraints(gridxCounter,gridyCounter);
460
    return(retval);
461
  }
462
  
463
  /**
464
   * Returns a newly created GridBagConstraints for the next column 
465
   * of the current layout row using the given parameters.
466
   * @param gridwidth width for this constraint
467
   * @param gridheight height for this constraint
468
   * @return a newly created GridBagConstraints for the next column 
469
   * of the current layout row using the given parameters
470
   */
471
  private GridBagConstraints getNextXGridBagConstraints( int gridwidth, int gridheight)
472
  {
473
    GridBagConstraints retval = getNextXGridBagConstraints();
474
    retval.gridwidth = gridwidth;
475
    retval.gridheight = gridheight;
476
    return( retval );
477
  }
478
  /**
479
   * Returns a newly created GridBagConstraints with column 0 
480
   * for the next row.
481
   * @return a newly created GridBagConstraints with column 0 
482
   * for the next row
483
   * 
484
   */
485
  public GridBagConstraints getNextYGridBagConstraints()
486
  {
487
    gridyCounter++;
488
    gridxCounter = 0;
489
    GridBagConstraints retval = getNewGridBagConstraints(0,gridyCounter);
490
    return(retval);
491
  }
492
  
493
  /**
494
   * Returns a newly created GridBagConstraints with column 0 
495
   * for the next row using the given parameters.
496
   * @param gridwidth width for this constraint
497
   * @param gridheight height for this constraint
498
   * @return a newly created GridBagConstraints with column 0 
499
   * for the next row using the given parameters
500
   */
501
  public GridBagConstraints getNextYGridBagConstraints( int gridwidth, int gridheight)
502
  {
503
    startGridBagLayout();
504
    GridBagConstraints retval = getNextYGridBagConstraints();
505
    retval.gridwidth = gridwidth;
506
    retval.gridheight = gridheight;
507
    return( retval );
508
  }
509
  
510
  /**
511
   * Start layout determining. If it is needed, a dummy component 
512
   * will be created as first row. This will be done,
513
   * if the IzPack variable <code>IzPanel.LayoutType</code> has
514
   * the value "BOTTOM".
515
   */
516
  public void startGridBagLayout()
517
  {
518
    if( gridBagLayoutStarted  )
519
      return;
520
    gridBagLayoutStarted = true;
521
    GridBagLayout layout = new GridBagLayout();
522
    defaultGridBagConstraints.insets = new Insets( 0, 0, 20, 0);
523
    defaultGridBagConstraints.anchor = GridBagConstraints.WEST;   
524
    setLayout(layout);
525
    String todo = idata.getVariable("IzPanel.LayoutType");
526
    if( todo == null )  // No command, no work.
527
      return;
528
    if( todo.equals("BOTTOM"))
529
    { // Make a header to push the rest to the bottom.
530
      Filler dummy = new Filler();
531
      GridBagConstraints gbConstraint = getNextYGridBagConstraints();
532
      gbConstraint.weighty = 1.0;
533
      gbConstraint.fill = GridBagConstraints.BOTH;
534
      gbConstraint.anchor = GridBagConstraints.WEST;        
535
      this.add(dummy, gbConstraint );
536
    }
537
  
538
    // TODO: impl for layout type CENTER,  ...
539
  }
540
  
541

    
542
  /**
543
   * Complete layout determining. If it is needed, a dummy component 
544
   * will be created as last row. This will be done,
545
   * if the IzPack variable <code>IzPanel.LayoutType</code> has
546
   * the value "TOP".
547
   */
548
  public void completeGridBagLayout()
549
  {
550
    String todo = idata.getVariable("IzPanel.LayoutType");
551
    if( todo == null )  // No command, no work.
552
      return;
553
    if( todo.equals("TOP"))
554
    { // Make a footer to push the rest to the top.
555
       Filler dummy = new Filler();
556
      GridBagConstraints gbConstraint = getNextYGridBagConstraints();
557
      gbConstraint.weighty = 1.0;
558
      gbConstraint.fill = GridBagConstraints.BOTH;
559
      gbConstraint.anchor = GridBagConstraints.WEST;        
560
      this.add(dummy, gbConstraint );
561
    }
562
  }
563
    
564
  //------------------- Layout stuff -------------------- END ---
565

    
566

    
567
  //------------------- Inner classes ------------------- START ---
568
  public static class Filler extends JComponent 
569
  {
570
    
571
  }
572
  //------------------- Inner classes ------------------- END ---
573

    
574

    
575
}