001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.log4j.lf5.viewer; 018 019import java.awt.Component; 020import java.awt.Container; 021import java.awt.Dimension; 022import java.awt.Font; 023import java.awt.GridBagConstraints; 024import java.awt.GridBagLayout; 025import java.awt.Insets; 026import java.awt.Label; 027import java.awt.Toolkit; 028import java.awt.Window; 029 030import javax.swing.JDialog; 031import javax.swing.JFrame; 032 033/** 034 * LogFactor5Dialog 035 * 036 * @author Richard Hurst 037 * @author Brad Marlborough 038 */ 039 040// Contributed by ThoughtWorks Inc. 041 042public abstract class LogFactor5Dialog extends JDialog { 043 //-------------------------------------------------------------------------- 044 // Constants: 045 //-------------------------------------------------------------------------- 046 protected static final Font DISPLAY_FONT = new Font("Arial", Font.BOLD, 12); 047 //-------------------------------------------------------------------------- 048 // Protected Variables: 049 //-------------------------------------------------------------------------- 050 051 //-------------------------------------------------------------------------- 052 // Private Variables: 053 //-------------------------------------------------------------------------- 054 055 //-------------------------------------------------------------------------- 056 // Constructors: 057 //-------------------------------------------------------------------------- 058 protected LogFactor5Dialog(JFrame jframe, String message, boolean modal) { 059 super(jframe, message, modal); 060 } 061 062 //-------------------------------------------------------------------------- 063 // Public Methods: 064 //-------------------------------------------------------------------------- 065 public void show() { 066 pack(); 067 minimumSizeDialog(this, 200, 100); 068 centerWindow(this); 069 super.show(); 070 } 071 072 //-------------------------------------------------------------------------- 073 // Protected Methods: 074 //-------------------------------------------------------------------------- 075 076 //-------------------------------------------------------------------------- 077 // Private Methods: 078 //-------------------------------------------------------------------------- 079 protected void centerWindow(Window win) { 080 Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); 081 082 // If larger than screen, reduce window width or height 083 if (screenDim.width < win.getSize().width) { 084 win.setSize(screenDim.width, win.getSize().height); 085 } 086 087 if (screenDim.height < win.getSize().height) { 088 win.setSize(win.getSize().width, screenDim.height); 089 } 090 091 // Center Frame, Dialogue or Window on screen 092 int x = (screenDim.width - win.getSize().width) / 2; 093 int y = (screenDim.height - win.getSize().height) / 2; 094 win.setLocation(x, y); 095 } 096 097 protected void wrapStringOnPanel(String message, 098 Container container) { 099 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}