Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: dtk/gui/TKWidgetMaker.java


1   //Title:        D3E ToolKit 
2   //Version:       
3   //Copyright:    Copyright (c) 1999 
4   //Author:       John Weatherley
5   //Company:       
6   //Description:  D3E Toolkit 
7   
8   package dtk.gui;  
9   
10  import dtk.core.*;  
11  import java.awt.*; 
12  import javax.swing.*; 
13  import java.io.*;
14  import java.net.*;
15  
16  /**
17  This class creates common widgets used throughout the Toolkit GUI. It 
18  also has some all-purpose utility methods (see {@link dtk.gui.TKUtil}
19  for more).
20  
21  @see  dtk.gui.TKUtil
22  */
23  public class TKWidgetMaker
24  {
25    // Directory offset to the button images:
26    static final String fileRootOffset = Constants.fileRootOffset;
27    static final String buttonOffset = Constants.buttonOffset;
28    static final String imageOffset = Constants.imageOffset;
29  
30    static ImageIcon helpUp;
31    static ImageIcon helpDown;
32    static ImageIcon blank; 
33    static ImageIcon addressBookBullet;
34    static ImageIcon blueArrow;
35    static JLabel blueArrowLabel;
36    static Dimension spacer5 = new Dimension(5,5);
37    static Dimension spacer2 = new Dimension(2,2);
38  
39    /**
40    Constructor does some one-time class static initializations
41    accross all instantiations.
42    */ 
43    TKWidgetMaker (){
44      // Inititlize once for all instances:
45      if(helpUp == null){ // Check if stuff already initialized
46        helpUp = getImageIcon(buttonOffset + "helpup.gif");
47        helpDown = getImageIcon(buttonOffset + "helpdown.gif");
48        blank = getImageIcon(buttonOffset + "blank.gif"); // A transparant image
49        addressBookBullet = getImageIcon(imageOffset + "addressBookBullet.gif");
50        blueArrow = getImageIcon(imageOffset + "blueArrow.gif");
51        blueArrowLabel = new JLabel(blueArrow);
52      }
53    }
54  
55    /**The directory offset to the common buttons. Offered for convenience.*/
56    public String getButtonOffset() { return buttonOffset; }
57    /**The directory offset to the common images. Offered for convenience.*/
58    public String getImageOffset() { return imageOffset; }
59    /**The directory offset to the file root. Offered for convenience.*/
60    public String getFileRootOffset() { return fileRootOffset; }
61  
62    /**
63    Check to see if a file exists.
64    Returns true if the file f exists locally in the file system or in the 
65    toolkit's jar file, otherwise returns false.
66    
67    @param  f  The offset to the file.
68    */
69    public boolean fileExists(String f){
70        try {
71        // Check to see if the file exists:
72        if(new File(f).exists()) {
73          return true;        
74        } else {
75          // If the file doesn't exist, try looking in the jar file:
76          f = "/" + f; // Need leading slash
77  
78          // Set up URL path into jar file:
79          URL theURL = getClass().getResource(f);
80  
81          if(theURL != null) return true;
82        }
83  
84      } catch (Exception e) {
85        return false;
86      }
87      return false;
88    }
89  
90    /**
91    Returns a Toolkit help button.
92    */
93    public JButton getHelpButton(){
94        JButton helpButton = new JButton(helpUp);
95         helpButton.setPressedIcon(helpDown);
96        helpButton.setFocusPainted(false);
97        helpButton.setBorderPainted(false);
98        helpButton.setContentAreaFilled(false);
99        helpButton.setMargin(new Insets(0,0,0,0));
100       return helpButton;
101   }
102 
103   /**
104   Return a blank (transparant) button that's the same size as the help button.
105   Used as a spacer.
106   */
107   public JButton getBlankButton(){
108       JButton blankButton = new JButton(blank);
109       blankButton.setFocusPainted(false);
110       blankButton.setBorderPainted(false);
111       blankButton.setContentAreaFilled(false);
112       blankButton.setMargin(new Insets(0,0,0,0));
113       blankButton.setRequestFocusEnabled(false);
114       return blankButton;
115   }
116 
117   /**
118   Return a horizontal panel that holds three components - used to put
119   a help button on the left with another component on the right. If the first parameter is
120   null, then blank space will be placed on the left.
121   */
122   public JPanel threeComponentHorizPanel(JComponent left,JComponent right,JComponent farRight,int width,int height){
123     JPanel p = new JPanel();
124 
125     setComponentSize(right,width,height);
126 
127     p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
128     p.setAlignmentX(JPanel.LEFT_ALIGNMENT);
129 
130     if(left != null)
131       p.add(left);
132     else
133       p.add(getBlankButton()); // Spacer
134     p.add(Box.createRigidArea(spacer5));
135     p.add(right);
136     p.add(Box.createRigidArea(spacer2));
137     if(farRight != null)
138       p.add(farRight);
139     p.add(Box.createHorizontalGlue());
140     return p;
141   }
142 
143   /**
144   Return a tookit image by first checking for it in the local directory,
145   then going to the jar if it's not here locally.
146   */
147   public ImageIcon getImageIcon(String path){
148 
149     try {
150       // Check to see if the file exists:
151       File file = new File(path);
152       if(file.exists()) {
153         return new ImageIcon(path);        
154       } else {
155         // If the file doesn't exist, grab from the jar file:
156         path = "/" + path; // Need leading slash
157 
158         // Set up URL path into jar file:
159         URL theURL = getClass().getResource(path);
160 
161         return new ImageIcon(theURL);
162       }
163 
164     } catch (Exception e) {
165       System.err.println("Couldn't find the following file: " + path);
166     }
167     return new ImageIcon();
168   }
169 
170 
171   /**
172   Set the size of a component.
173   
174   @param  widget  The component to size.
175   @param  w    The desired width for the component.
176   @param  h    The desired height for the component.
177   */
178   public void setComponentSize(JComponent widget, int w, int h){
179     Dimension d = new Dimension(w,h);
180     widget.setPreferredSize(d);
181     widget.setMaximumSize(d);
182     widget.setMinimumSize(d);
183   }
184 }