1 /*
   2  *
   3  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 import javax.swing.*;
  34 import javax.swing.text.*;
  35 import javax.swing.border.*;
  36 
  37 import java.awt.*;
  38 import java.awt.event.*;
  39 import java.util.*;
  40 
  41 /*
  42  * The LayoutControlPanel contains controls for setting an
  43  * AbstractButton's horizontal and vertical text position and
  44  * horizontal and vertical alignment.
  45  */
  46 
  47 public class LayoutControlPanel extends JPanel implements SwingConstants {
  48 
  49     private boolean  absolutePositions;
  50     private DirectionPanel textPosition = null;
  51     private DirectionPanel labelAlignment = null;
  52     private ButtonDemo demo = null;
  53 
  54     // private ComponentOrientChanger componentOrientChanger = null;
  55 
  56     LayoutControlPanel(ButtonDemo demo) {
  57         this.demo = demo;
  58 
  59         // this.componentOrientationChanger = componentOrientationChanger;
  60 
  61         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  62         setAlignmentX(LEFT_ALIGNMENT);
  63         setAlignmentY(TOP_ALIGNMENT);
  64 
  65         JLabel l;
  66 
  67         // If SwingSet has a ComponentOrientationChanger, then include control
  68         // for choosing between absolute and relative positioning.  This will
  69         // only happen when we're running on JDK 1.2 or above.
  70         //
  71         // if(componentOrientationChanger != null ) {
  72         //     l = new JLabel("Positioning:");
  73         //     add(l);
  74         //
  75         //    ButtonGroup group = new ButtonGroup();
  76         //    PositioningListener positioningListener = new PositioningListener();
  77         //    JRadioButton absolutePos = new JRadioButton("Absolute");
  78         //    absolutePos.setMnemonic('a');
  79         //    absolutePos.setToolTipText("Text/Content positioning is independant of line direction");
  80         //    group.add(absolutePos);
  81         //    absolutePos.addItemListener(positioningListener);
  82         //    add(absolutePos);
  83         //
  84         //    JRadioButton relativePos = new JRadioButton("Relative");
  85         //    relativePos.setMnemonic('r');
  86         //    relativePos.setToolTipText("Text/Content positioning depends on line direction.");
  87         //    group.add(relativePos);
  88         //    relativePos.addItemListener(positioningListener);
  89         //    add(relativePos);
  90         //
  91         //    add(Box.createRigidArea(demo.VGAP20));
  92         //
  93         //    absolutePositions = false;
  94         //    relativePos.setSelected(true);
  95         //
  96         //    componentOrientationChanger.addActionListener( new OrientationChangeListener() );
  97         //} else {
  98             absolutePositions = true;
  99         //}
 100 
 101         textPosition = new DirectionPanel(true, "E", new TextPositionListener());
 102         labelAlignment = new DirectionPanel(true, "C", new LabelAlignmentListener());
 103 
 104         // Make sure the controls' text position and label alignment match
 105         // the initial value of the associated direction panel.
 106         for(int i = 0; i < demo.getCurrentControls().size(); i++) {
 107             Component c = (Component) demo.getCurrentControls().elementAt(i);
 108             setPosition(c, RIGHT, CENTER);
 109             setAlignment(c,CENTER,CENTER);
 110         }
 111 
 112         l = new JLabel(demo.getString("LayoutControlPanel.textposition_label"));
 113         add(l);
 114         add(textPosition);
 115 
 116         add(Box.createRigidArea(demo.VGAP20));
 117 
 118         l = new JLabel(demo.getString("LayoutControlPanel.contentalignment_label"));
 119         add(l);
 120         add(labelAlignment);
 121 
 122         add(Box.createGlue());
 123     }
 124 
 125 
 126     class OrientationChangeListener implements ActionListener {
 127         public void actionPerformed( ActionEvent e ) {
 128             if( !e.getActionCommand().equals("OrientationChanged") ){
 129                 return;
 130             }
 131             if( absolutePositions ){
 132                 return;
 133             }
 134 
 135             String currentTextPosition = textPosition.getSelection();
 136             if( currentTextPosition.equals("NW") )
 137                 textPosition.setSelection("NE");
 138             else if( currentTextPosition.equals("NE") )
 139                 textPosition.setSelection("NW");
 140             else if( currentTextPosition.equals("E") )
 141                 textPosition.setSelection("W");
 142             else if( currentTextPosition.equals("W") )
 143                 textPosition.setSelection("E");
 144             else if( currentTextPosition.equals("SE") )
 145                 textPosition.setSelection("SW");
 146             else if( currentTextPosition.equals("SW") )
 147                 textPosition.setSelection("SE");
 148 
 149             String currentLabelAlignment = labelAlignment.getSelection();
 150             if( currentLabelAlignment.equals("NW") )
 151                 labelAlignment.setSelection("NE");
 152             else if( currentLabelAlignment.equals("NE") )
 153                 labelAlignment.setSelection("NW");
 154             else if( currentLabelAlignment.equals("E") )
 155                 labelAlignment.setSelection("W");
 156             else if( currentLabelAlignment.equals("W") )
 157                 labelAlignment.setSelection("E");
 158             else if( currentLabelAlignment.equals("SE") )
 159                 labelAlignment.setSelection("SW");
 160             else if( currentLabelAlignment.equals("SW") )
 161                 labelAlignment.setSelection("SE");
 162         }
 163     }
 164 
 165     class PositioningListener implements ItemListener {
 166 
 167         public void itemStateChanged(ItemEvent e) {
 168             JRadioButton rb = (JRadioButton) e.getSource();
 169             if(rb.getText().equals("Absolute") && rb.isSelected()) {
 170                 absolutePositions = true;
 171             } else if(rb.getText().equals("Relative") && rb.isSelected()) {
 172                 absolutePositions = false;
 173             }
 174 
 175             for(int i = 0; i < demo.getCurrentControls().size(); i++) {
 176                 Component c = (Component) demo.getCurrentControls().elementAt(i);
 177                 int hPos, vPos, hAlign, vAlign;
 178                 if( c instanceof AbstractButton ) {
 179                    hPos = ((AbstractButton)c).getHorizontalTextPosition();
 180                    vPos = ((AbstractButton)c).getVerticalTextPosition();
 181                    hAlign = ((AbstractButton)c).getHorizontalAlignment();
 182                    vAlign = ((AbstractButton)c).getVerticalAlignment();
 183                 } else if( c instanceof JLabel ) {
 184                    hPos = ((JLabel)c).getHorizontalTextPosition();
 185                    vPos = ((JLabel)c).getVerticalTextPosition();
 186                    hAlign = ((JLabel)c).getHorizontalAlignment();
 187                    vAlign = ((JLabel)c).getVerticalAlignment();
 188                 } else {
 189                     continue;
 190                 }
 191                 setPosition(c, hPos, vPos);
 192                 setAlignment(c, hAlign, vAlign);
 193             }
 194 
 195             demo.invalidate();
 196             demo.validate();
 197             demo.repaint();
 198         }
 199     };
 200 
 201 
 202     // Text Position Listener
 203     class TextPositionListener implements ActionListener {
 204         public void actionPerformed(ActionEvent e) {
 205             JRadioButton rb = (JRadioButton) e.getSource();
 206             if(!rb.isSelected()) {
 207                 return;
 208             }
 209             String cmd = rb.getActionCommand();
 210             int hPos, vPos;
 211             if(cmd.equals("NW")) {
 212                     hPos = LEFT; vPos = TOP;
 213             } else if(cmd.equals("N")) {
 214                     hPos = CENTER; vPos = TOP;
 215             } else if(cmd.equals("NE")) {
 216                     hPos = RIGHT; vPos = TOP;
 217             } else if(cmd.equals("W")) {
 218                     hPos = LEFT; vPos = CENTER;
 219             } else if(cmd.equals("C")) {
 220                     hPos = CENTER; vPos = CENTER;
 221             } else if(cmd.equals("E")) {
 222                     hPos = RIGHT; vPos = CENTER;
 223             } else if(cmd.equals("SW")) {
 224                     hPos = LEFT; vPos = BOTTOM;
 225             } else if(cmd.equals("S")) {
 226                     hPos = CENTER; vPos = BOTTOM;
 227             } else /*if(cmd.equals("SE"))*/ {
 228                     hPos = RIGHT; vPos = BOTTOM;
 229             }
 230             for(int i = 0; i < demo.getCurrentControls().size(); i++) {
 231                 Component c = (Component) demo.getCurrentControls().elementAt(i);
 232                 setPosition(c, hPos, vPos);
 233             }
 234             demo.invalidate();
 235             demo.validate();
 236             demo.repaint();
 237         }
 238     };
 239 
 240 
 241     // Label Alignment Listener
 242     class LabelAlignmentListener implements  ActionListener {
 243         public void actionPerformed(ActionEvent e) {
 244             JRadioButton rb = (JRadioButton) e.getSource();
 245             if(!rb.isSelected()) {
 246                 return;
 247             }
 248             String cmd = rb.getActionCommand();
 249             int hPos, vPos;
 250             if(cmd.equals("NW")) {
 251                     hPos = LEFT; vPos = TOP;
 252             } else if(cmd.equals("N")) {
 253                     hPos = CENTER; vPos = TOP;
 254             } else if(cmd.equals("NE")) {
 255                     hPos = RIGHT; vPos = TOP;
 256             } else if(cmd.equals("W")) {
 257                     hPos = LEFT; vPos = CENTER;
 258             } else if(cmd.equals("C")) {
 259                     hPos = CENTER; vPos = CENTER;
 260             } else if(cmd.equals("E")) {
 261                     hPos = RIGHT; vPos = CENTER;
 262             } else if(cmd.equals("SW")) {
 263                     hPos = LEFT; vPos = BOTTOM;
 264             } else if(cmd.equals("S")) {
 265                     hPos = CENTER; vPos = BOTTOM;
 266             } else /*if(cmd.equals("SE"))*/ {
 267                     hPos = RIGHT; vPos = BOTTOM;
 268             }
 269             for(int i = 0; i < demo.getCurrentControls().size(); i++) {
 270                 Component c = (Component) demo.getCurrentControls().elementAt(i);
 271                 setAlignment(c,hPos,vPos);
 272                 c.invalidate();
 273             }
 274             demo.invalidate();
 275             demo.validate();
 276             demo.repaint();
 277         }
 278     };
 279 
 280     // Position
 281     void setPosition(Component c, int hPos, int vPos) {
 282         boolean ltr = true;
 283         ltr = c.getComponentOrientation().isLeftToRight();
 284         if( absolutePositions ) {
 285             if( hPos == LEADING ) {
 286                 hPos = ltr ? LEFT : RIGHT;
 287             } else if( hPos == TRAILING ) {
 288                 hPos = ltr ? RIGHT : LEFT;
 289             }
 290         } else {
 291             if( hPos == LEFT ) {
 292                 hPos = ltr ? LEADING : TRAILING;
 293             } else if( hPos == RIGHT ) {
 294                 hPos = ltr ? TRAILING : LEADING;
 295             }
 296         }
 297         if(c instanceof AbstractButton) {
 298             AbstractButton x = (AbstractButton) c;
 299             x.setHorizontalTextPosition(hPos);
 300             x.setVerticalTextPosition(vPos);
 301         } else if(c instanceof JLabel) {
 302             JLabel x = (JLabel) c;
 303             x.setHorizontalTextPosition(hPos);
 304             x.setVerticalTextPosition(vPos);
 305         }
 306     }
 307 
 308     void setAlignment(Component c, int hPos, int vPos) {
 309         boolean ltr = true;
 310         ltr = c.getComponentOrientation().isLeftToRight();
 311         if( absolutePositions ) {
 312             if( hPos == LEADING ) {
 313                 hPos = ltr ? LEFT : RIGHT;
 314             } else if( hPos == TRAILING ) {
 315                 hPos = ltr ? RIGHT : LEFT;
 316             }
 317         } else {
 318             if( hPos == LEFT ) {
 319                 hPos = ltr ? LEADING : TRAILING;
 320             } else if( hPos == RIGHT ) {
 321                 hPos = ltr ? TRAILING : LEADING;
 322             }
 323         }
 324         if(c instanceof AbstractButton) {
 325             AbstractButton x = (AbstractButton) c;
 326             x.setHorizontalAlignment(hPos);
 327             x.setVerticalAlignment(vPos);
 328         } else if(c instanceof JLabel) {
 329             JLabel x = (JLabel) c;
 330             x.setHorizontalAlignment(hPos);
 331             x.setVerticalAlignment(vPos);
 332         }
 333     }
 334 }