1 20 package com.izforge.izpack.installer; 21 22 import com.izforge.izpack.Panel; 23 import com.izforge.izpack.gui.LabelFactory; 24 import com.izforge.izpack.gui.LayoutConstants; 25 import com.izforge.izpack.util.AbstractUIHandler; 26 import com.izforge.izpack.util.Debug; 27 import com.izforge.izpack.util.MultiLineLabel; 28 import com.izforge.izpack.util.VariableSubstitutor; 29 import net.n3.nanoxml.XMLElement; 30 31 import javax.swing.*; 32 import javax.swing.plaf.metal.MetalLookAndFeel ; 33 import java.awt.*; 34 35 51 public class IzPanel extends JPanel implements AbstractUIHandler, LayoutConstants 52 { 53 54 private static final long serialVersionUID = 3256442495255786038L; 55 56 57 protected LayoutHelper layoutHelper; 58 59 60 protected Component initialFocus = null; 61 62 65 protected InstallData idata; 66 67 68 protected InstallerFrame parent; 69 70 71 protected String myFullClassname; 72 73 74 protected String myClassname; 75 76 77 protected String myPrefix; 78 79 80 protected String headline; 81 82 83 protected JLabel headLineLabel; 84 85 86 protected boolean hidden; 87 88 89 public final static String HEADLINE = "headline"; 90 91 92 public final static int X_ORIGIN = 0; 93 94 95 public final static int Y_ORIGIN = 0; 96 97 public final static String D = "."; 98 99 100 public final static String d = D; 101 102 103 public final static int COLS_1 = 1; 104 105 106 public final static int ROWS_1 = 1; 107 108 111 public Panel metadata; 112 113 119 public IzPanel(InstallerFrame parent, InstallData idata) 120 { 121 this( parent, idata, (LayoutManager2) null); 122 } 123 124 132 public IzPanel(InstallerFrame parent, InstallData idata, LayoutManager2 lm) 133 { 134 super(); 135 init( parent, idata ); 136 if( lm != null ) 137 getLayoutHelper().startLayout(lm); 138 } 139 140 147 public IzPanel( InstallerFrame parent, InstallData idata, String iconName ) 148 { 149 this( parent, idata, iconName, -1 ); 150 } 151 152 160 public IzPanel( InstallerFrame parent, InstallData idata, String iconName, int instance ) 161 { 162 this(parent, idata); 163 buildHeadline( iconName, instance ); 164 } 165 166 180 protected boolean buildHeadline( String imageIconName, int instanceNumber ) 181 { 182 boolean result = false; 183 if( parent.isHeading(this)) 184 return(false); 185 186 String headline; 191 String headlineSearchBaseKey = myClassname + d + "headline"; 193 if( instanceNumber > -1 ) { 195 String instanceSearchKey = headlineSearchBaseKey + d + 196 Integer.toString( instanceNumber ); 197 198 String instanceHeadline = getString( instanceSearchKey ); 199 200 if( Debug.isLOG() ) 201 { 202 System.out.println( "found headline: " + instanceHeadline + d + " for instance # " + instanceNumber ); 203 } 204 if( ! instanceSearchKey.equals( instanceHeadline ) ) 205 { 206 headline = instanceHeadline; 207 } 208 else 209 { 210 headline = getString( headlineSearchBaseKey ); 211 } 212 } 213 else 214 { 215 headline = getString( headlineSearchBaseKey ); 216 } 217 218 if( headline != null ) 219 { 220 if( ( imageIconName != null ) && ! "".equals( imageIconName ) ) 221 { 222 headLineLabel = new JLabel( headline, getImageIcon( imageIconName ), 223 SwingConstants.LEADING ); 224 } 225 else 226 { 227 headLineLabel = new JLabel( headline ); 228 } 229 230 Font font = headLineLabel.getFont( ); 231 float size = font.getSize( ); 232 int style = 0; 233 font = font.deriveFont( style, ( size * 1.5f ) ); 234 headLineLabel.setFont( font ); 235 236 GridBagConstraints gbc = new GridBagConstraints( ); 237 238 gbc.gridx = 0; 239 gbc.gridy = 0; 240 gbc.gridwidth = 1; 241 gbc.gridheight = 1; 242 243 gbc.fill = GridBagConstraints.HORIZONTAL; 244 gbc.anchor = GridBagConstraints.WEST; 245 gbc.insets = new Insets( 0, 0, 0, 0 ); 246 headLineLabel.setName( HEADLINE ); 247 ((GridBagLayout) getLayout()).addLayoutComponent( headLineLabel, gbc ); 248 249 add( headLineLabel ); 250 } 251 252 return result; 253 } 254 255 262 public String getString( String key ) 263 { 264 return parent.langpack.getString( key ); 265 } 266 267 274 public ImageIcon getImageIcon( String iconName ) 275 { 276 return parent.icons.getImageIcon( iconName ); 277 } 278 279 280 281 284 protected void initLayoutHelper() 285 { 286 layoutHelper = new LayoutHelper(this); 287 } 288 289 290 293 protected void getClassName( ) 294 { 295 myFullClassname = getClass( ).getName( ); 296 myClassname = myFullClassname.substring( myFullClassname.lastIndexOf( "." ) + 1 ); 297 myPrefix = myClassname + "."; 298 } 299 300 306 protected void init( InstallerFrame parent, InstallData idata ) 307 { 308 getClassName( ); 309 310 this.idata = idata; 311 this.parent = parent; 312 this.metadata = idata.currentPanel; 321 idata.currentPanel = null; 322 initLayoutHelper( ); 323 324 } 325 326 333 public boolean isValidated() 334 { 335 return true; 336 } 337 338 343 public void panelActivate() 344 { 345 } 346 347 351 public void panelDeactivate() 352 { 353 } 354 355 361 public void makeXMLData(XMLElement panelRoot) 362 { 363 } 364 365 376 public int askQuestion(String title, String question, int choices) 377 { 378 return askQuestion(title, question, choices, -1); 379 } 380 381 392 public int askQuestion(String title, String question, int choices, int default_choice) 393 { 394 int jo_choices = 0; 395 396 if (choices == AbstractUIHandler.CHOICES_YES_NO) 397 jo_choices = JOptionPane.YES_NO_OPTION; 398 else if (choices == AbstractUIHandler.CHOICES_YES_NO_CANCEL) 399 jo_choices = JOptionPane.YES_NO_CANCEL_OPTION; 400 401 int user_choice = JOptionPane.showConfirmDialog(this, (Object ) question, title, jo_choices, 402 JOptionPane.QUESTION_MESSAGE); 403 404 if (user_choice == JOptionPane.CANCEL_OPTION) return AbstractUIHandler.ANSWER_CANCEL; 405 406 if (user_choice == JOptionPane.YES_OPTION) return AbstractUIHandler.ANSWER_YES; 407 408 if (user_choice == JOptionPane.CLOSED_OPTION) return AbstractUIHandler.ANSWER_NO; 409 410 if (user_choice == JOptionPane.NO_OPTION) return AbstractUIHandler.ANSWER_NO; 411 412 return default_choice; 413 } 414 415 public boolean emitNotificationFeedback(String message) 416 { 417 return (JOptionPane.showConfirmDialog(this, message, idata.langpack.getString("installer.Message"), JOptionPane.WARNING_MESSAGE, 418 JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION); 419 } 420 421 426 public void emitNotification(String message) 427 { 428 JOptionPane.showMessageDialog(this, message); 429 } 430 431 436 public boolean emitWarning(String title, String message) 437 { 438 return (JOptionPane.showConfirmDialog(this, message, title, JOptionPane.WARNING_MESSAGE, 439 JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION); 440 441 } 442 443 448 public void emitError(String title, String message) 449 { 450 JOptionPane.showMessageDialog(this, message, title, JOptionPane.ERROR_MESSAGE); 451 } 452 453 458 public Component getInitialFocus() 459 { 460 return initialFocus; 461 } 462 463 468 public void setInitialFocus(Component component) 469 { 470 initialFocus = component; 471 } 472 473 482 public String getI18nStringForClass(String subkey) 483 { 484 String retval = null; 485 Class clazz = this.getClass(); 486 while (retval == null && !clazz.getName().endsWith(".IzPanel")) 487 { 488 retval = getI18nStringForClass(clazz.getName(), subkey, null); 489 clazz = clazz.getSuperclass(); 490 } 491 return (retval); 492 } 493 494 504 public String getI18nStringForClass(String subkey, String alternateClass) 505 { 506 return( getI18nStringForClass(getClass().getName(), subkey, alternateClass)); 507 508 } 509 510 private String getI18nStringForClass(String curClassName, String subkey, String alternateClass) 511 { 512 513 int nameStart = curClassName.lastIndexOf('.') + 1; 514 curClassName = curClassName.substring(nameStart, curClassName.length()); 515 StringBuffer buf = new StringBuffer (); 516 buf.append(curClassName).append(".").append(subkey); 517 String fullkey = buf.toString(); 518 String panelid = null; 519 if( getMetadata() != null ) 520 { 521 panelid = getMetadata().getPanelid(); 522 } 523 String retval = null; 524 if (panelid != null) 525 { 526 buf.append("."); 527 buf.append(panelid); 528 retval = parent.langpack.getString(buf.toString()); 529 } 530 if (retval == null || retval.startsWith(fullkey)) 531 retval = parent.langpack.getString(fullkey); 532 if (retval == null || retval.startsWith(fullkey)) 533 { 534 if (alternateClass == null) return (null); 535 buf.delete(0, buf.length()); 536 buf.append(alternateClass).append(".").append(subkey); 537 retval = parent.langpack.getString(buf.toString()); 538 } 539 if (retval != null && retval.indexOf('$') > -1) 540 { 541 VariableSubstitutor substitutor = new VariableSubstitutor(idata.getVariables()); 542 retval = substitutor.substitute(retval, null); 543 } 544 return (retval); 545 } 546 547 548 553 public InstallerFrame getInstallerFrame() 554 { 555 return (parent); 556 } 557 558 560 571 public JLabel createLabel(String subkey, String alternateClass, String iconId, int pos) 572 { 573 ImageIcon ii = (iconId != null) ? parent.icons.getImageIcon(iconId) : null; 574 String msg = getI18nStringForClass(subkey, alternateClass); 575 JLabel label = LabelFactory.create(msg, ii, pos); 576 if (label != null) label.setFont(getControlTextFont()); 577 return (label); 578 579 } 580 581 595 public JLabel createLabel(String subkey, String alternateClass, String iconId, int pos, 596 boolean isFullLine) 597 { 598 ImageIcon ii = (iconId != null) ? parent.icons.getImageIcon(iconId) : null; 599 String msg = getI18nStringForClass(subkey, alternateClass); 600 JLabel label = LabelFactory.create(msg, ii, pos, isFullLine); 601 if (label != null) label.setFont(getControlTextFont()); 602 return (label); 603 604 } 605 606 616 public JLabel createLabel(String textId, String iconId, int pos) 617 { 618 return (createLabel(textId, iconId, pos, false)); 619 } 620 621 634 public JLabel createLabel(String textId, String iconId, int pos, boolean isFullLine) 635 { 636 ImageIcon ii = (iconId != null) ? parent.icons.getImageIcon(iconId) : null; 637 JLabel label = LabelFactory.create(parent.langpack.getString(textId), ii, pos, isFullLine); 638 if (label != null) label.setFont(getControlTextFont()); 639 return (label); 640 641 } 642 643 651 public MultiLineLabel createMultiLineLabelLang(String textId) 652 { 653 return (createMultiLineLabel(parent.langpack.getString(textId))); 654 } 655 656 662 public MultiLineLabel createMultiLineLabel(String text) 663 { 664 return (createMultiLineLabel(text, null, SwingConstants.LEFT)); 665 } 666 667 677 public MultiLineLabel createMultiLineLabel(String text, String iconId, int pos) 678 { 679 MultiLineLabel mll = null; 680 mll = new MultiLineLabel(text, 0, 0); 681 if (mll != null) mll.setFont(getControlTextFont()); 682 return (mll); 683 } 684 685 688 public Font getControlTextFont() 689 { 690 return (getLAF() != null ? MetalLookAndFeel.getControlTextFont() : getFont()); 691 } 692 693 protected static MetalLookAndFeel getLAF() 694 { 695 LookAndFeel laf = UIManager.getLookAndFeel(); 696 if (laf instanceof MetalLookAndFeel ) return ((MetalLookAndFeel ) laf); 697 return (null); 698 } 699 700 708 public GridBagConstraints getDefaultGridBagConstraints() 709 { 710 return(GridBagConstraints) ( layoutHelper.getDefaultConstraints()); 711 } 712 713 719 public void setDefaultGridBagConstraints(GridBagConstraints constraints) 720 { 721 layoutHelper.setDefaultConstraints(constraints); 722 } 723 724 729 public void resetGridCounter() 730 { 731 layoutHelper.resetGridCounter(); 732 } 733 734 744 public GridBagConstraints getNewGridBagConstraints(int gridx, int gridy) 745 { 746 return(GridBagConstraints) ( layoutHelper.getNewConstraints(gridx, gridy)); 747 } 748 749 761 public GridBagConstraints getNewGridBagConstraints(int gridx, int gridy, int gridwidth, 762 int gridheight) 763 { 764 return(GridBagConstraints) (layoutHelper.getNewConstraints(gridx, gridy, gridwidth, gridheight)); 765 } 766 767 774 public GridBagConstraints getNextXGridBagConstraints() 775 { 776 return(GridBagConstraints) (layoutHelper.getNextXConstraints()); 777 } 778 779 786 public GridBagConstraints getNextYGridBagConstraints() 787 { 788 return(GridBagConstraints) (layoutHelper.getNextYConstraints()); 789 } 790 791 801 public GridBagConstraints getNextYGridBagConstraints(int gridwidth, int gridheight) 802 { 803 return(GridBagConstraints) (layoutHelper.getNextYConstraints(gridwidth, gridheight)); 804 } 805 806 813 public void startGridBagLayout() 814 { 815 layoutHelper.startLayout(new GridBagLayout()); 816 } 817 818 825 public void completeGridBagLayout() 826 { 827 layoutHelper.completeLayout(); 828 } 829 830 832 842 public String getSummaryBody() 843 { 844 return null; 845 } 846 847 856 public String getSummaryCaption() 857 { 858 String caption; 859 if (parent.isHeading(this) 860 && idata.guiPrefs.modifier.containsKey("useHeadingForSummary") 861 && ((String ) idata.guiPrefs.modifier.get("useHeadingForSummary")) 862 .equalsIgnoreCase("yes")) 863 { 864 caption = getI18nStringForClass("headline", this.getClass().getName()); 865 } 866 else 867 caption = getI18nStringForClass("summaryCaption", this.getClass().getName()); 868 869 return (caption); 870 } 871 872 874 public static class Filler extends JComponent 876 { 877 878 private static final long serialVersionUID = 3258416144414095153L; 879 880 } 881 883 884 890 public boolean isHidden() 891 { 892 return hidden; 893 } 894 895 896 902 public void setHidden(boolean hidden) 903 { 904 this.hidden = hidden; 905 } 906 907 908 913 public LayoutHelper getLayoutHelper() 914 { 915 return layoutHelper; 916 } 917 918 921 public Panel getMetadata() { 922 return this.metadata; 923 } 924 925 928 public void setMetadata(Panel p) { 929 this.metadata = p; 930 } 931 } 932 | Popular Tags |