View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.log4j.lf5.viewer;
18  
19  import java.awt.Component;
20  import java.awt.Container;
21  import java.awt.Dimension;
22  import java.awt.Font;
23  import java.awt.GridBagConstraints;
24  import java.awt.GridBagLayout;
25  import java.awt.Insets;
26  import java.awt.Label;
27  import java.awt.Toolkit;
28  import java.awt.Window;
29  
30  import javax.swing.JDialog;
31  import javax.swing.JFrame;
32  
33  /**
34   * LogFactor5Dialog
35   *
36   * @author Richard Hurst
37   * @author Brad Marlborough
38   */
39  
40  // Contributed by ThoughtWorks Inc.
41  
42  public abstract class LogFactor5Dialog extends JDialog {
43    //--------------------------------------------------------------------------
44    //   Constants:
45    //--------------------------------------------------------------------------
46    protected static final Font DISPLAY_FONT = new Font("Arial", Font.BOLD, 12);
47    //--------------------------------------------------------------------------
48    //   Protected Variables:
49    //--------------------------------------------------------------------------
50  
51    //--------------------------------------------------------------------------
52    //   Private Variables:
53    //--------------------------------------------------------------------------
54  
55    //--------------------------------------------------------------------------
56    //   Constructors:
57    //--------------------------------------------------------------------------
58    protected LogFactor5Dialog(JFrame jframe, String message, boolean modal) {
59      super(jframe, message, modal);
60    }
61  
62    //--------------------------------------------------------------------------
63    //   Public Methods:
64    //--------------------------------------------------------------------------
65    public void show() {
66      pack();
67      minimumSizeDialog(this, 200, 100);
68      centerWindow(this);
69      super.show();
70    }
71  
72    //--------------------------------------------------------------------------
73    //   Protected Methods:
74    //--------------------------------------------------------------------------
75  
76    //--------------------------------------------------------------------------
77    //   Private Methods:
78    //--------------------------------------------------------------------------
79    protected void centerWindow(Window win) {
80      Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
81  
82      // If larger than screen, reduce window width or height
83      if (screenDim.width < win.getSize().width) {
84        win.setSize(screenDim.width, win.getSize().height);
85      }
86  
87      if (screenDim.height < win.getSize().height) {
88        win.setSize(win.getSize().width, screenDim.height);
89      }
90  
91      // Center Frame, Dialogue or Window on screen
92      int x = (screenDim.width - win.getSize().width) / 2;
93      int y = (screenDim.height - win.getSize().height) / 2;
94      win.setLocation(x, y);
95    }
96  
97    protected void wrapStringOnPanel(String message,
98        Container container) {
99      GridBagConstraints c = getDefaultConstraints();
100     c.gridwidth = GridBagConstraints.REMAINDER;
101     // Insets() args are top, left, bottom, right
102     c.insets = new Insets(0, 0, 0, 0);
103     GridBagLayout gbLayout = (GridBagLayout) container.getLayout();
104 
105 
106     while (message.length() > 0) {
107       int newLineIndex = message.indexOf('\n');
108       String line;
109       if (newLineIndex >= 0) {
110         line = message.substring(0, newLineIndex);
111         message = message.substring(newLineIndex + 1);
112       } else {
113         line = message;
114         message = "";
115       }
116       Label label = new Label(line);
117       label.setFont(DISPLAY_FONT);
118       gbLayout.setConstraints(label, c);
119       container.add(label);
120     }
121   }
122 
123   protected GridBagConstraints getDefaultConstraints() {
124     GridBagConstraints constraints = new GridBagConstraints();
125     constraints.weightx = 1.0;
126     constraints.weighty = 1.0;
127     constraints.gridheight = 1; // One row high
128     // Insets() args are top, left, bottom, right
129     constraints.insets = new Insets(4, 4, 4, 4);
130     // fill of NONE means do not change size
131     constraints.fill = GridBagConstraints.NONE;
132     // WEST means align left
133     constraints.anchor = GridBagConstraints.WEST;
134 
135     return constraints;
136   }
137 
138   protected void minimumSizeDialog(Component component,
139       int minWidth,
140       int minHeight) {
141     // set the min width
142     if (component.getSize().width < minWidth)
143       component.setSize(minWidth, component.getSize().height);
144     // set the min height
145     if (component.getSize().height < minHeight)
146       component.setSize(component.getSize().width, minHeight);
147   }
148   //--------------------------------------------------------------------------
149   //   Nested Top-Level Classes or Interfaces
150   //--------------------------------------------------------------------------
151 }