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 javax.swing.*;
    20  import java.awt.*;
    21
    22  /***
    23   * LogFactor5Dialog
    24   *
    25   * @author Richard Hurst
    26   * @author Brad Marlborough
    27   */
    28
    29  // Contributed by ThoughtWorks Inc.
    30
    31  public abstract class LogFactor5Dialog extends JDialog {
    32    //--------------------------------------------------------------------------
    33    //   Constants:
    34    //--------------------------------------------------------------------------
    35    protected static final Font DISPLAY_FONT = new Font("Arial", Font.BOLD, 12);
    36    //--------------------------------------------------------------------------
    37    //   Protected Variables:
    38    //--------------------------------------------------------------------------
    39
    40    //--------------------------------------------------------------------------
    41    //   Private Variables:
    42    //--------------------------------------------------------------------------
    43
    44    //--------------------------------------------------------------------------
    45    //   Constructors:
    46    //--------------------------------------------------------------------------
    47    protected LogFactor5Dialog(JFrame jframe, String message, boolean modal) {
    48      super(jframe, message, modal);
    49    }
    50
    51    //--------------------------------------------------------------------------
    52    //   Public Methods:
    53    //--------------------------------------------------------------------------
    54    public void show() {
    55      pack();
    56      minimumSizeDialog(this, 200, 100);
    57      centerWindow(this);
    58      super.show();
    59    }
    60
    61    //--------------------------------------------------------------------------
    62    //   Protected Methods:
    63    //--------------------------------------------------------------------------
    64
    65    //--------------------------------------------------------------------------
    66    //   Private Methods:
    67    //--------------------------------------------------------------------------
    68    protected void centerWindow(Window win) {
    69      Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
    70
    71      // If larger than screen, reduce window width or height
    72      if (screenDim.width < win.getSize().width) {
    73        win.setSize(screenDim.width, win.getSize().height);
    74      }
    75
    76      if (screenDim.height < win.getSize().height) {
    77        win.setSize(win.getSize().width, screenDim.height);
    78      }
    79
    80      // Center Frame, Dialogue or Window on screen
    81      int x = (screenDim.width - win.getSize().width) / 2;
    82      int y = (screenDim.height - win.getSize().height) / 2;
    83      win.setLocation(x, y);
    84    }
    85
    86    protected void wrapStringOnPanel(String message,
    87        Container container) {
    88      GridBagConstraints c = getDefaultConstraints();
    89      c.gridwidth = GridBagConstraints.REMAINDER;
    90      // Insets() args are top, left, bottom, right
    91      c.insets = new Insets(0, 0, 0, 0);
    92      GridBagLayout gbLayout = (GridBagLayout) container.getLayout();
    93
    94
    95      while (message.length() > 0) {
    96        int newLineIndex = message.indexOf('\n');
    97        String line;
    98        if (newLineIndex >= 0) {
    99          line = message.substring(0, newLineIndex);
    100         message = message.substring(newLineIndex + 1);
    101       } else {
    102         line = message;
    103         message = "";
    104       }
    105       Label label = new Label(line);
    106       label.setFont(DISPLAY_FONT);
    107       gbLayout.setConstraints(label, c);
    108       container.add(label);
    109     }
    110   }
    111
    112   protected GridBagConstraints getDefaultConstraints() {
    113     GridBagConstraints constraints = new GridBagConstraints();
    114     constraints.weightx = 1.0;
    115     constraints.weighty = 1.0;
    116     constraints.gridheight = 1; // One row high
    117     // Insets() args are top, left, bottom, right
    118     constraints.insets = new Insets(4, 4, 4, 4);
    119     // fill of NONE means do not change size
    120     constraints.fill = GridBagConstraints.NONE;
    121     // WEST means align left
    122     constraints.anchor = GridBagConstraints.WEST;
    123
    124     return constraints;
    125   }
    126
    127   protected void minimumSizeDialog(Component component,
    128       int minWidth,
    129       int minHeight) {
    130     // set the min width
    131     if (component.getSize().width < minWidth)
    132       component.setSize(minWidth, component.getSize().height);
    133     // set the min height
    134     if (component.getSize().height < minHeight)
    135       component.setSize(component.getSize().width, minHeight);
    136   }
    137   //--------------------------------------------------------------------------
    138   //   Nested Top-Level Classes or Interfaces
    139   //--------------------------------------------------------------------------
    140 }