Path: blob/master/src/demo/share/jfc/SwingSet2/LayoutControlPanel.java
41152 views
/*1*2* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132import javax.swing.*;33import javax.swing.text.*;34import javax.swing.border.*;3536import java.awt.*;37import java.awt.event.*;38import java.util.*;3940/*41* The LayoutControlPanel contains controls for setting an42* AbstractButton's horizontal and vertical text position and43* horizontal and vertical alignment.44*/4546public class LayoutControlPanel extends JPanel implements SwingConstants {4748private boolean absolutePositions;49private DirectionPanel textPosition = null;50private DirectionPanel labelAlignment = null;51private ButtonDemo demo = null;5253// private ComponentOrientChanger componentOrientChanger = null;5455LayoutControlPanel(ButtonDemo demo) {56this.demo = demo;5758// this.componentOrientationChanger = componentOrientationChanger;5960setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));61setAlignmentX(LEFT_ALIGNMENT);62setAlignmentY(TOP_ALIGNMENT);6364JLabel l;6566// If SwingSet has a ComponentOrientationChanger, then include control67// for choosing between absolute and relative positioning. This will68// only happen when we're running on JDK 1.2 or above.69//70// if(componentOrientationChanger != null ) {71// l = new JLabel("Positioning:");72// add(l);73//74// ButtonGroup group = new ButtonGroup();75// PositioningListener positioningListener = new PositioningListener();76// JRadioButton absolutePos = new JRadioButton("Absolute");77// absolutePos.setMnemonic('a');78// absolutePos.setToolTipText("Text/Content positioning is independant of line direction");79// group.add(absolutePos);80// absolutePos.addItemListener(positioningListener);81// add(absolutePos);82//83// JRadioButton relativePos = new JRadioButton("Relative");84// relativePos.setMnemonic('r');85// relativePos.setToolTipText("Text/Content positioning depends on line direction.");86// group.add(relativePos);87// relativePos.addItemListener(positioningListener);88// add(relativePos);89//90// add(Box.createRigidArea(demo.VGAP20));91//92// absolutePositions = false;93// relativePos.setSelected(true);94//95// componentOrientationChanger.addActionListener( new OrientationChangeListener() );96//} else {97absolutePositions = true;98//}99100textPosition = new DirectionPanel(true, "E", new TextPositionListener());101labelAlignment = new DirectionPanel(true, "C", new LabelAlignmentListener());102103// Make sure the controls' text position and label alignment match104// the initial value of the associated direction panel.105for(int i = 0; i < demo.getCurrentControls().size(); i++) {106Component c = demo.getCurrentControls().elementAt(i);107setPosition(c, RIGHT, CENTER);108setAlignment(c,CENTER,CENTER);109}110111l = new JLabel(demo.getString("LayoutControlPanel.textposition_label"));112add(l);113add(textPosition);114115add(Box.createRigidArea(demo.VGAP20));116117l = new JLabel(demo.getString("LayoutControlPanel.contentalignment_label"));118add(l);119add(labelAlignment);120121add(Box.createGlue());122}123124125class OrientationChangeListener implements ActionListener {126public void actionPerformed( ActionEvent e ) {127if( !e.getActionCommand().equals("OrientationChanged") ){128return;129}130if( absolutePositions ){131return;132}133134String currentTextPosition = textPosition.getSelection();135if( currentTextPosition.equals("NW") )136textPosition.setSelection("NE");137else if( currentTextPosition.equals("NE") )138textPosition.setSelection("NW");139else if( currentTextPosition.equals("E") )140textPosition.setSelection("W");141else if( currentTextPosition.equals("W") )142textPosition.setSelection("E");143else if( currentTextPosition.equals("SE") )144textPosition.setSelection("SW");145else if( currentTextPosition.equals("SW") )146textPosition.setSelection("SE");147148String currentLabelAlignment = labelAlignment.getSelection();149if( currentLabelAlignment.equals("NW") )150labelAlignment.setSelection("NE");151else if( currentLabelAlignment.equals("NE") )152labelAlignment.setSelection("NW");153else if( currentLabelAlignment.equals("E") )154labelAlignment.setSelection("W");155else if( currentLabelAlignment.equals("W") )156labelAlignment.setSelection("E");157else if( currentLabelAlignment.equals("SE") )158labelAlignment.setSelection("SW");159else if( currentLabelAlignment.equals("SW") )160labelAlignment.setSelection("SE");161}162}163164class PositioningListener implements ItemListener {165166public void itemStateChanged(ItemEvent e) {167JRadioButton rb = (JRadioButton) e.getSource();168if(rb.getText().equals("Absolute") && rb.isSelected()) {169absolutePositions = true;170} else if(rb.getText().equals("Relative") && rb.isSelected()) {171absolutePositions = false;172}173174for(int i = 0; i < demo.getCurrentControls().size(); i++) {175Component c = demo.getCurrentControls().elementAt(i);176int hPos, vPos, hAlign, vAlign;177if( c instanceof AbstractButton ) {178hPos = ((AbstractButton)c).getHorizontalTextPosition();179vPos = ((AbstractButton)c).getVerticalTextPosition();180hAlign = ((AbstractButton)c).getHorizontalAlignment();181vAlign = ((AbstractButton)c).getVerticalAlignment();182} else if( c instanceof JLabel ) {183hPos = ((JLabel)c).getHorizontalTextPosition();184vPos = ((JLabel)c).getVerticalTextPosition();185hAlign = ((JLabel)c).getHorizontalAlignment();186vAlign = ((JLabel)c).getVerticalAlignment();187} else {188continue;189}190setPosition(c, hPos, vPos);191setAlignment(c, hAlign, vAlign);192}193194demo.invalidate();195demo.validate();196demo.repaint();197}198};199200201// Text Position Listener202class TextPositionListener implements ActionListener {203public void actionPerformed(ActionEvent e) {204JRadioButton rb = (JRadioButton) e.getSource();205if(!rb.isSelected()) {206return;207}208String cmd = rb.getActionCommand();209int hPos, vPos;210if(cmd.equals("NW")) {211hPos = LEFT; vPos = TOP;212} else if(cmd.equals("N")) {213hPos = CENTER; vPos = TOP;214} else if(cmd.equals("NE")) {215hPos = RIGHT; vPos = TOP;216} else if(cmd.equals("W")) {217hPos = LEFT; vPos = CENTER;218} else if(cmd.equals("C")) {219hPos = CENTER; vPos = CENTER;220} else if(cmd.equals("E")) {221hPos = RIGHT; vPos = CENTER;222} else if(cmd.equals("SW")) {223hPos = LEFT; vPos = BOTTOM;224} else if(cmd.equals("S")) {225hPos = CENTER; vPos = BOTTOM;226} else /*if(cmd.equals("SE"))*/ {227hPos = RIGHT; vPos = BOTTOM;228}229for(int i = 0; i < demo.getCurrentControls().size(); i++) {230Component c = demo.getCurrentControls().elementAt(i);231setPosition(c, hPos, vPos);232}233demo.invalidate();234demo.validate();235demo.repaint();236}237};238239240// Label Alignment Listener241class LabelAlignmentListener implements ActionListener {242public void actionPerformed(ActionEvent e) {243JRadioButton rb = (JRadioButton) e.getSource();244if(!rb.isSelected()) {245return;246}247String cmd = rb.getActionCommand();248int hPos, vPos;249if(cmd.equals("NW")) {250hPos = LEFT; vPos = TOP;251} else if(cmd.equals("N")) {252hPos = CENTER; vPos = TOP;253} else if(cmd.equals("NE")) {254hPos = RIGHT; vPos = TOP;255} else if(cmd.equals("W")) {256hPos = LEFT; vPos = CENTER;257} else if(cmd.equals("C")) {258hPos = CENTER; vPos = CENTER;259} else if(cmd.equals("E")) {260hPos = RIGHT; vPos = CENTER;261} else if(cmd.equals("SW")) {262hPos = LEFT; vPos = BOTTOM;263} else if(cmd.equals("S")) {264hPos = CENTER; vPos = BOTTOM;265} else /*if(cmd.equals("SE"))*/ {266hPos = RIGHT; vPos = BOTTOM;267}268for(int i = 0; i < demo.getCurrentControls().size(); i++) {269Component c = demo.getCurrentControls().elementAt(i);270setAlignment(c,hPos,vPos);271c.invalidate();272}273demo.invalidate();274demo.validate();275demo.repaint();276}277};278279// Position280void setPosition(Component c, int hPos, int vPos) {281boolean ltr = true;282ltr = c.getComponentOrientation().isLeftToRight();283if( absolutePositions ) {284if( hPos == LEADING ) {285hPos = ltr ? LEFT : RIGHT;286} else if( hPos == TRAILING ) {287hPos = ltr ? RIGHT : LEFT;288}289} else {290if( hPos == LEFT ) {291hPos = ltr ? LEADING : TRAILING;292} else if( hPos == RIGHT ) {293hPos = ltr ? TRAILING : LEADING;294}295}296if(c instanceof AbstractButton) {297AbstractButton x = (AbstractButton) c;298x.setHorizontalTextPosition(hPos);299x.setVerticalTextPosition(vPos);300} else if(c instanceof JLabel) {301JLabel x = (JLabel) c;302x.setHorizontalTextPosition(hPos);303x.setVerticalTextPosition(vPos);304}305}306307void setAlignment(Component c, int hPos, int vPos) {308boolean ltr = true;309ltr = c.getComponentOrientation().isLeftToRight();310if( absolutePositions ) {311if( hPos == LEADING ) {312hPos = ltr ? LEFT : RIGHT;313} else if( hPos == TRAILING ) {314hPos = ltr ? RIGHT : LEFT;315}316} else {317if( hPos == LEFT ) {318hPos = ltr ? LEADING : TRAILING;319} else if( hPos == RIGHT ) {320hPos = ltr ? TRAILING : LEADING;321}322}323if(c instanceof AbstractButton) {324AbstractButton x = (AbstractButton) c;325x.setHorizontalAlignment(hPos);326x.setVerticalAlignment(vPos);327} else if(c instanceof JLabel) {328JLabel x = (JLabel) c;329x.setHorizontalAlignment(hPos);330x.setVerticalAlignment(vPos);331}332}333}334335336