KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > lf5 > viewer > LogFactor5Dialog


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.log4j.lf5.viewer;
17
18 import javax.swing.*;
19 import java.awt.*;
20
21 /**
22  * LogFactor5Dialog
23  *
24  * @author Richard Hurst
25  * @author Brad Marlborough
26  */

27
28 // Contributed by ThoughtWorks Inc.
29

30 public abstract class LogFactor5Dialog extends JDialog {
31   //--------------------------------------------------------------------------
32
// Constants:
33
//--------------------------------------------------------------------------
34
protected static final Font DISPLAY_FONT = new Font("Arial", Font.BOLD, 12);
35   //--------------------------------------------------------------------------
36
// Protected Variables:
37
//--------------------------------------------------------------------------
38

39   //--------------------------------------------------------------------------
40
// Private Variables:
41
//--------------------------------------------------------------------------
42

43   //--------------------------------------------------------------------------
44
// Constructors:
45
//--------------------------------------------------------------------------
46
protected LogFactor5Dialog(JFrame jframe, String JavaDoc message, boolean modal) {
47     super(jframe, message, modal);
48   }
49
50   //--------------------------------------------------------------------------
51
// Public Methods:
52
//--------------------------------------------------------------------------
53
public void show() {
54     pack();
55     minimumSizeDialog(this, 200, 100);
56     centerWindow(this);
57     super.show();
58   }
59
60   //--------------------------------------------------------------------------
61
// Protected Methods:
62
//--------------------------------------------------------------------------
63

64   //--------------------------------------------------------------------------
65
// Private Methods:
66
//--------------------------------------------------------------------------
67
protected void centerWindow(Window win) {
68     Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
69
70     // If larger than screen, reduce window width or height
71
if (screenDim.width < win.getSize().width) {
72       win.setSize(screenDim.width, win.getSize().height);
73     }
74
75     if (screenDim.height < win.getSize().height) {
76       win.setSize(win.getSize().width, screenDim.height);
77     }
78
79     // Center Frame, Dialogue or Window on screen
80
int x = (screenDim.width - win.getSize().width) / 2;
81     int y = (screenDim.height - win.getSize().height) / 2;
82     win.setLocation(x, y);
83   }
84
85   protected void wrapStringOnPanel(String JavaDoc message,
86       Container container) {
87     GridBagConstraints c = getDefaultConstraints();
88     c.gridwidth = GridBagConstraints.REMAINDER;
89     // Insets() args are top, left, bottom, right
90
c.insets = new Insets(0, 0, 0, 0);
91     GridBagLayout gbLayout = (GridBagLayout) container.getLayout();
92
93
94     while (message.length() > 0) {
95       int newLineIndex = message.indexOf('\n');
96       String JavaDoc line;
97       if (newLineIndex >= 0) {
98         line = message.substring(0, newLineIndex);
99         message = message.substring(newLineIndex + 1);
100       } else {
101         line = message;
102         message = "";
103       }
104       Label label = new Label(line);
105       label.setFont(DISPLAY_FONT);
106       gbLayout.setConstraints(label, c);
107       container.add(label);
108     }
109   }
110
111   protected GridBagConstraints getDefaultConstraints() {
112     GridBagConstraints constraints = new GridBagConstraints();
113     constraints.weightx = 1.0;
114     constraints.weighty = 1.0;
115     constraints.gridheight = 1; // One row high
116
// Insets() args are top, left, bottom, right
117
constraints.insets = new Insets(4, 4, 4, 4);
118     // fill of NONE means do not change size
119
constraints.fill = GridBagConstraints.NONE;
120     // WEST means align left
121
constraints.anchor = GridBagConstraints.WEST;
122
123     return constraints;
124   }
125
126   protected void minimumSizeDialog(Component component,
127       int minWidth,
128       int minHeight) {
129     // set the min width
130
if (component.getSize().width < minWidth)
131       component.setSize(minWidth, component.getSize().height);
132     // set the min height
133
if (component.getSize().height < minHeight)
134       component.setSize(component.getSize().width, minHeight);
135   }
136   //--------------------------------------------------------------------------
137
// Nested Top-Level Classes or Interfaces
138
//--------------------------------------------------------------------------
139
}
Popular Tags