Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/gui/imprt/ImportToPanel.java


1   /*
2    * ImportToPanel.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:30 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.gui.imprt;
12  
13  import java.awt.GridBagConstraints;
14  import java.awt.GridBagLayout;
15  import java.awt.Insets;
16  import java.awt.Label;
17  import java.awt.Panel;
18  import java.awt.event.ItemEvent;
19  import java.awt.event.ItemListener;
20  import java.util.Vector;
21  
22  import com.flexstor.common.awt.AWTUtil;
23  import com.flexstor.common.awt.FlexBorderPanel;
24  import com.flexstor.common.awt.field.FlexChoice;
25  import com.flexstor.common.disguise.DisguiseUtils;
26  import com.flexstor.common.modules.registry.BaseInstanceI;
27  import com.flexstor.common.resources.Resources;
28  import com.flexstor.common.settings.ImportSettingsI;
29  
30  
31  
32  public class ImportToPanel extends FlexBorderPanel
33     implements java.util.Observer, ItemListener
34  {
35     protected Label lApplication;
36     protected FlexChoice chkApplication;
37     protected Label lValues;
38     protected FlexChoice chkValues;
39     protected GridBagLayout gbl      = new GridBagLayout();
40     protected GridBagConstraints gbc = new GridBagConstraints();
41     protected SettingsModelI sModel  = null;
42     protected String         appLabel= "";
43     protected BaseInstanceI currentAsset = null;
44  
45     protected Vector treeData = null;
46  
47     public ImportToPanel( SettingsModelI sModel )
48     {
49        super( Resources.get( 6542 ) );
50        this.sModel   = sModel;
51  
52        setLayout ( new java.awt.BorderLayout() );
53        Panel panel = getPanel();
54        add ( "West", panel );
55     }
56  
57     private Panel getPanel()
58     {
59        gbl      = new GridBagLayout();
60        gbc      = new GridBagConstraints();
61  
62        Panel panel = new Panel();
63  
64        panel.setLayout ( gbl );
65   
66        lApplication = new Label(Resources.get(6587));
67        chkApplication = new FlexChoice();
68        lValues = new Label(Resources.get(6541));
69        chkValues = new FlexChoice();
70  
71        gbc = AWTUtil.getGbc( 0, 0, 0, 0, gbc.WEST, gbc.NONE );
72        gbc.insets = new Insets( 4, 8, 4, 4 );
73        gbl.setConstraints( lApplication, gbc );
74        panel.add( lApplication );
75  
76        gbc = AWTUtil.getGbc( 1, 0, 0, 0, gbc.WEST, gbc.NONE );
77        gbc.insets = new Insets( 4, 25, 4, 4 );
78        gbl.setConstraints( chkApplication, gbc );
79        panel.add( chkApplication );
80  
81        gbc = AWTUtil.getGbc( 0, 1, 0, 0, gbc.WEST, gbc.NONE );
82        gbc.insets = new Insets( 4, 8, 4, 4 );
83        gbl.setConstraints( lValues, gbc );
84        panel.add( lValues );
85  
86        gbc = AWTUtil.getGbc( 1, 1, 0, 0, gbc.WEST, gbc.NONE );
87        gbc.insets = new Insets( 4, 25, 4, 4 );
88        gbl.setConstraints( chkValues, gbc );
89        panel.add( chkValues );
90  
91        chkApplication.addItemListener ( this );
92  
93        return panel;
94     }
95  
96     public void loadData()
97     {
98  
99       if ( sModel == null )
100        return;
101 
102      String[] dNames = sModel.getDisguiseNames();
103 
104      if ( dNames != null )
105      {
106        for ( int i=0; i<dNames.length; i++ )
107        {
108         if ( !appChkContains ( dNames[ i ] ) )
109         {
110           chkApplication.add ( dNames[ i ] );
111         }
112        }
113 
114        /*
115 
116        // temp testing block
117        if ( !appChkContains ( "Fake Disguise 1" ) )
118         chkApplication.add ("Fake Disguise 1");
119        if ( !appChkContains ( "Fake Disguise 2" ) )
120         chkApplication.add ("Fake Disguise 2");
121        // temp testing block
122        */
123        
124 
125 
126        if ( sModel.isFromRetrieval () )
127        {
128          initValues();
129        }
130        else if ( chkApplication.countItems() > 0 )
131        {
132         if ( sModel.getCurrentImportSetting() != null )
133         {
134           String disguiseNameInCtlFile =
135               sModel.getCurrentImportSetting().getString( ImportSettingsI.APPLICATION_NAME );
136 
137           if ( !appChkContains ( disguiseNameInCtlFile ) )
138           {
139             // either the disguise in the ctl file is not assigned to the user or its not SYSTEM disguise
140             // We change the disguise name on the fly, depending, from where
141             // the import frame is brought up. But since the changes are not done from the user
142             // it should not show the dialog saying 'Things are changed, wanna save ?'.
143             sModel.setCurrentDisguiseName ( chkApplication.getItem ( 0 ), true, true );
144 
145           }
146           else
147           {
148             chkApplication.select ( disguiseNameInCtlFile );
149           }
150         }
151         else
152         {
153           sModel.setCurrentDisguiseName ( chkApplication.getItem ( 0 ), true, true );
154         }
155         chkValues.setEnabled ( false );
156        }
157      }
158    }
159 
160    private void initValues()
161    {
162       BaseInstanceI baseInstance  = sModel.getCurrentAsset();
163       if ( baseInstance != null )
164       {
165         String disguiseName = getDisguiseName( baseInstance );
166 
167         if ( ( disguiseName != null ) &&
168              ( disguiseName.trim().length() != 0 ) )
169         {
170           if ( !appChkContains ( disguiseName ) )
171           {
172             chkApplication.addItem ( disguiseName );
173           }
174           chkApplication.select ( disguiseName );
175           if ( sModel.getCurrentImportSetting() != null )
176           {
177             // its on the fly hence the last parameter is true
178             sModel.setCurrentDisguiseName ( disguiseName, true, true );
179           }
180         }
181 
182 
183         chkApplication.setEnabled( false );
184 
185         Vector values   = getTreeValues  ( baseInstance );
186 
187         if ( values != null )
188         {
189           chkValues.removeAll();
190           String blank = new String ("");
191           for ( int i=0; i<values.size(); i++ )
192           {
193             chkValues.addItem ( blank.concat ( ( String ) values.elementAt ( i ) ) );
194             blank = blank.concat (" ");
195           }
196         }
197       }
198    }
199    private boolean appChkContains ( String name )
200    {
201      if ( ( name == null ) || ( chkApplication.countItems() <= 0 ) )
202        return false;
203 
204      for ( int i=0; i<chkApplication.countItems(); i++ )
205      {
206        if ( chkApplication.getItem( i ).trim().equals ( name.trim() ) )
207           return true;
208      }
209 
210      return false;
211    }
212 
213    public void setTextField( String s )
214    {
215       chkApplication.setEnabled ( true );
216       chkApplication.select ( s );
217       chkApplication.setEnabled ( false );
218    }
219 
220    /** Checks the State of the Place in To Do checkbox
221    /  @return The boolean state of the Checkbox
222    */
223    public boolean isToDoChecked() // no longer a valid method
224    {
225       return false;
226    }
227 
228    public boolean isCopyEnabled() // no longer a valid method
229    {
230       return false;
231    }
232    public void update( java.util.Observable observable, Object object )
233    {
234       if ( sModel == null )
235         return;
236 
237       Notification cmd = (Notification)object;
238 
239       String     command = ( String )cmd.getCommand();
240       if ( command.equals ( ImportSettingsI.SETTING_CHANGED ) )
241       {
242        loadData();
243       }
244    }
245    public void itemStateChanged ( ItemEvent event )
246    {
247      if ( sModel == null )
248        return;
249 
250      Object object = event.getSource();
251      if ( object instanceof FlexChoice )
252      {
253        FlexChoice choice = ( FlexChoice )object;
254        if ( choice == chkApplication )
255        {
256          // Param1 : The selected disguise
257          // Param2 : Should observers be notified ?
258          // Param3 : Is this change On the fly ? 'On the Flies' are the changes which
259          //          are not done by the user from the UI, these are the changes we need
260          //          to do in the ctl for the proper functionings.
261          sModel.setCurrentDisguiseName ( chkApplication.getSelectedItem(), true, false );
262        }
263      }
264    }
265    private String getDisguiseName( BaseInstanceI instance )
266    {
267      String disguiseName = null;
268      if ( instance.getTraversalPath() != null )
269      {
270       try
271       {
272         disguiseName = DisguiseUtils.getDisguiseName ( instance.getTraversalPath().getDisguiseId() );
273       }
274       catch ( Exception ex ){}
275      }
276      return disguiseName;
277    }
278    private Vector getTreeValues (  BaseInstanceI instance  )
279    {
280       Vector values = new Vector();
281 
282       if ( instance == null )
283         return values;
284 
285       values.addElement ( instance.getLabel() );
286 
287       BaseInstanceI binst = instance.getParentInstance();
288       
289       while ( !binst.getInstanceId().isSystemInstance() )
290       {
291             values.addElement ( binst.getLabel() );
292             binst = binst.getParentInstance();
293       }
294       return values;
295    }
296    public void configureComponents ()
297    {
298    }
299 }
300 
301