jdk/make/tools/swing-nimbus/classes/org/jdesktop/synthdesigner/synthmodel/UIRegion.java
changeset 3754 41a9e8c0158c
parent 3725 b2169a6c9c86
parent 3753 c0c9b5f2c874
child 3755 683ea3f13029
equal deleted inserted replaced
3725:b2169a6c9c86 3754:41a9e8c0158c
     1 /*
       
     2  * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 package org.jdesktop.synthdesigner.synthmodel;
       
    26 
       
    27 import org.jdesktop.beans.AbstractBean;
       
    28 import org.jdesktop.swingx.designer.utils.HasUIDefaults;
       
    29 import org.jibx.runtime.IUnmarshallingContext;
       
    30 
       
    31 import javax.swing.UIDefaults;
       
    32 import java.awt.Insets;
       
    33 import java.beans.PropertyChangeEvent;
       
    34 import java.beans.PropertyChangeListener;
       
    35 import java.util.List;
       
    36 import java.util.ArrayList;
       
    37 
       
    38 /**
       
    39  * Represents a "Region" in synth, which also includes entire components.
       
    40  *
       
    41  * @author  Richard Bair
       
    42  * @author  Jasper Potts
       
    43  */
       
    44 public class UIRegion extends AbstractBean implements HasUIDefaults, HasUIStyle {
       
    45     private String name;//the code-wise name of the region
       
    46     protected String key; //the UIdefaults key for this region
       
    47     protected String title; //the user friendly name/title of this region
       
    48     /** List of background states */
       
    49     private List<UIState> backgroundStates;
       
    50     /** List of foreground states */
       
    51     private List<UIState> foregroundStates;
       
    52     /** List of border states */
       
    53     private List<UIState> borderStates;
       
    54     private UIStyle style = new UIStyle();
       
    55     protected Insets contentMargins = new Insets(0, 0, 0, 0);
       
    56     /** Sub regions, if any */
       
    57     private List<UIRegion> subRegions;
       
    58 
       
    59     //together with name, these two fields allow me to reconstruct, in
       
    60     //code, a synth Region, including a custom Region, if you make one.
       
    61     private String ui;
       
    62     private boolean subregion;
       
    63     /**
       
    64      * This is a local UIDefaults that contains all the UIDefaults in the synth model. It is kept uptodate by the
       
    65      * indervidual UIDefaults nodes
       
    66      */
       
    67     private transient UIDefaults modelDefaults = null;
       
    68 
       
    69     private UIRegion region; //the region that this region belongs to
       
    70 
       
    71     // =================================================================================================================
       
    72     // Constructors
       
    73 
       
    74     /** no-args contructor for JIBX */
       
    75     protected UIRegion() {
       
    76         subRegions = new ArrayList<UIRegion>();
       
    77         backgroundStates = new ArrayList<UIState>();
       
    78         foregroundStates = new ArrayList<UIState>();
       
    79         borderStates = new ArrayList<UIState>();
       
    80         style.addPropertyChangeListener(new PropertyChangeListener() {
       
    81             public void propertyChange(PropertyChangeEvent evt) {
       
    82                 firePropertyChange("style." + evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
       
    83             }
       
    84         });
       
    85     }
       
    86 
       
    87     public UIRegion(String name, UIRegion... subRegions) {
       
    88         this(name, null, true, subRegions);
       
    89     }
       
    90 
       
    91     public UIRegion(String name, String ui, UIRegion... subRegions) {
       
    92         this(name, ui, false, subRegions);
       
    93     }
       
    94 
       
    95     public UIRegion(String name, String ui, boolean subregion, UIRegion... subRegions) {
       
    96         this();
       
    97         this.name = name;
       
    98         this.ui = ui;
       
    99         this.subregion = subregion;
       
   100         if (subRegions != null) {
       
   101             for (UIRegion r : subRegions) {
       
   102                 if (r != null) {
       
   103                     this.subRegions.add(r);
       
   104                     r.getStyle().setParentStyle(getStyle());
       
   105                 }
       
   106             }
       
   107         }
       
   108     }
       
   109 
       
   110     // =================================================================================================================
       
   111     // JIBX Methods
       
   112 
       
   113     /**
       
   114      * Called by JIBX after all fields have been set
       
   115      *
       
   116      * @param context The JIBX Unmarshalling Context
       
   117      */
       
   118     private void preSet(IUnmarshallingContext context) {
       
   119         // walk up till we get synth model
       
   120         for (int i = 0; i < context.getStackDepth(); i++) {
       
   121             if (context.getStackObject(i) instanceof HasUIDefaults) {
       
   122                 modelDefaults = ((HasUIDefaults) context.getStackObject(i)).getUiDefaults();
       
   123                 if (modelDefaults != null) break;
       
   124             }
       
   125         }
       
   126         for (int i = 0; i < context.getStackDepth(); i++) {
       
   127             if (context.getStackObject(i) instanceof UIRegion && context.getStackObject(i) != this) {
       
   128                 region = (UIRegion) context.getStackObject(i);
       
   129                 break;
       
   130             }
       
   131         }
       
   132     }
       
   133 
       
   134     // =================================================================================================================
       
   135     // Bean Methods
       
   136 
       
   137     public Insets getContentMargins() {
       
   138         return contentMargins;
       
   139     }
       
   140 
       
   141     public void setContentMargins(Insets contentMargins) {
       
   142         Insets old = getContentMargins();
       
   143         this.contentMargins = contentMargins;
       
   144         firePropertyChange("contentMargins", old, getContentMargins());
       
   145     }
       
   146 
       
   147     void setRegion(UIRegion r) {
       
   148         this.region = r;
       
   149     }
       
   150 
       
   151     public UIRegion getRegion() {
       
   152         return region;
       
   153     }
       
   154 
       
   155     /**
       
   156      * Get the local UIDefaults that contains all the UIDefaults in the synth model. It is kept uptodate by the
       
   157      * indervidual UIDefaults nodes
       
   158      *
       
   159      * @return The UIDefaults for the synth model
       
   160      */
       
   161     public UIDefaults getUiDefaults() {
       
   162         return modelDefaults;
       
   163     }
       
   164 
       
   165     public String getName() {
       
   166         return name;
       
   167     }
       
   168 
       
   169     public final UIRegion[] getSubRegions() {
       
   170         return subRegions.toArray(new UIRegion[0]);
       
   171     }
       
   172 
       
   173     public final UIState[] getBackgroundStates() {
       
   174         return backgroundStates.toArray(new UIState[0]);
       
   175     }
       
   176 
       
   177     public final UIState[] getForegroundStates() {
       
   178         return foregroundStates.toArray(new UIState[0]);
       
   179     }
       
   180 
       
   181     public final UIState[] getBorderStates() {
       
   182         return borderStates.toArray(new UIState[0]);
       
   183     }
       
   184 
       
   185     public UIStyle getStyle() {
       
   186         return style;
       
   187     }
       
   188 
       
   189     public final boolean isSubRegion() {
       
   190         return subregion;
       
   191     }
       
   192 
       
   193     public final String getUi() {
       
   194         return ui;
       
   195     }
       
   196 
       
   197     public void addBackgroundState(UIState state) {
       
   198         // check if we already have that state
       
   199         for (UIState uiState : backgroundStates) {
       
   200             if (uiState.getName().equals(state.getName())) return;
       
   201         }
       
   202         backgroundStates.add(state);
       
   203         state.setRegion(this);
       
   204         firePropertyChange("backgroundStates", null, backgroundStates);
       
   205     }
       
   206 
       
   207     public void removeBackgroundState(UIState state) {
       
   208         if (backgroundStates.remove(state)) {
       
   209             firePropertyChange("backgroundStates", null, backgroundStates);
       
   210         }
       
   211     }
       
   212 
       
   213     public void addForegroundState(UIState state) {
       
   214         // check if we already have that state
       
   215         for (UIState uiState : foregroundStates) {
       
   216             if (uiState.getName().equals(state.getName())) return;
       
   217         }
       
   218         foregroundStates.add(state);
       
   219         state.setRegion(this);
       
   220         firePropertyChange("foregroundStates", null, foregroundStates);
       
   221     }
       
   222 
       
   223     public void removeForegroundState(UIState state) {
       
   224         if (foregroundStates.remove(state)) {
       
   225             firePropertyChange("foregroundStates", null, foregroundStates);
       
   226         }
       
   227     }
       
   228 
       
   229     public void addBorderState(UIState state) {
       
   230         // check if we already have that state
       
   231         for (UIState uiState : borderStates) {
       
   232             if (uiState.getName().equals(state.getName())) return;
       
   233         }
       
   234         borderStates.add(state);
       
   235         state.setRegion(this);
       
   236         firePropertyChange("borderStates", null, borderStates);
       
   237     }
       
   238 
       
   239     public void removeBorderState(UIState state) {
       
   240         if (borderStates.remove(state)) {
       
   241             firePropertyChange("borderStates", null, borderStates);
       
   242         }
       
   243     }
       
   244 
       
   245 
       
   246     public String getKey() {
       
   247         return key == null || "".equals(key) ? name : key;
       
   248     }
       
   249 
       
   250     public String getTitle() {
       
   251         return title == null || "".equals(title) ? name : title;
       
   252     }
       
   253 }