1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.wizards.ui;
24 
25 import com.sun.star.awt.XTextComponent;
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.uno.Exception;
28 import com.sun.star.wizards.common.FileAccess;
29 import com.sun.star.wizards.common.PropertyNames;
30 import com.sun.star.wizards.common.SystemDialog;
31 
32 public class PathSelection
33 {
34 
35     UnoDialog2 CurUnoDialog;
36     XMultiServiceFactory xMSF;
37     int iDialogType;
38     int iTransferMode;
39     public String sDefaultDirectory = PropertyNames.EMPTY_STRING;
40     public String sDefaultName = PropertyNames.EMPTY_STRING;
41     public String sDefaultFilter = PropertyNames.EMPTY_STRING;
42     public boolean usedPathPicker = false;
43     public XPathSelectionListener xAction;
44     public XTextComponent xSaveTextBox;
45     private final int CMDSELECTPATH = 1;
46     private final int TXTSAVEPATH = 1;
47 
48     public static class DialogTypes
49     {
50 
51         public static final int FOLDER = 0;
52         public static final int FILE = 1;
53     }
54 
55     public static class TransferMode
56     {
57 
58         public static final int SAVE = 0;
59         public static final int LOAD = 1;
60     }
61 
PathSelection(XMultiServiceFactory xMSF, UnoDialog2 CurUnoDialog, int TransferMode, int DialogType)62     public PathSelection(XMultiServiceFactory xMSF, UnoDialog2 CurUnoDialog, int TransferMode, int DialogType)
63     {
64         this.CurUnoDialog = CurUnoDialog;
65         this.xMSF = xMSF;
66         this.iDialogType = DialogType;
67         this.iTransferMode = TransferMode;
68 
69     }
70 
insert(int DialogStep, int XPos, int YPos, int Width, short CurTabIndex, String LabelText, boolean Enabled, String TxtHelpURL, String BtnHelpURL)71     public void insert(int DialogStep, int XPos, int YPos, int Width, short CurTabIndex, String LabelText, boolean Enabled, String TxtHelpURL, String BtnHelpURL)
72     {
73 
74         CurUnoDialog.insertControlModel("com.sun.star.awt.UnoControlFixedTextModel", "lblSaveAs", new String[]
75                 {
76                     PropertyNames.PROPERTY_ENABLED, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
77                 }, new Object[]
78                 {
79                         Boolean.valueOf(Enabled), 8, LabelText, new Integer(XPos), new Integer(YPos), new Integer(DialogStep), new Short(CurTabIndex), new Integer(Width)
80                 });
81 
82         xSaveTextBox = CurUnoDialog.insertTextField("txtSavePath", "callXPathSelectionListener", this, new String[]
83                 {
84                     PropertyNames.PROPERTY_ENABLED, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
85                 }, new Object[]
86                 {
87                         Boolean.valueOf(Enabled), 12, TxtHelpURL, new Integer(XPos), new Integer(YPos + 10), new Integer(DialogStep), new Short((short) (CurTabIndex + 1)), new Integer(Width - 26)
88                 });
89         //CurUnoDialog.setControlProperty("txtSavePath", PropertyNames.READ_ONLY, Boolean.TRUE);
90         CurUnoDialog.setControlProperty("txtSavePath", PropertyNames.PROPERTY_ENABLED, Boolean.FALSE);
91         CurUnoDialog.insertButton("cmdSelectPath", "triggerPathPicker", this, new String[]
92                 {
93                     PropertyNames.PROPERTY_ENABLED, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
94                 }, new Object[]
95                 {
96                         Boolean.valueOf(Enabled), 14, BtnHelpURL, "...", new Integer(XPos + Width - 16), new Integer(YPos + 9), new Integer(DialogStep), new Short((short) (CurTabIndex + 2)), 16
97                 });
98 
99     }
100 
addSelectionListener(XPathSelectionListener xAction)101     public void addSelectionListener(XPathSelectionListener xAction)
102     {
103         this.xAction = xAction;
104     }
105 
getSelectedPath()106     public String getSelectedPath()
107     {
108         return xSaveTextBox.getText();
109     }
110 
initializePath()111     public void initializePath()
112     {
113         try
114         {
115             FileAccess myFA = new FileAccess(xMSF);
116             xSaveTextBox.setText(myFA.getPath(sDefaultDirectory + "/" + sDefaultName, null));
117         }
118         catch (Exception e)
119         {
120             e.printStackTrace();
121         }
122     }
123 
triggerPathPicker()124     public void triggerPathPicker()
125     {
126         try
127         {
128             switch (iTransferMode)
129             {
130                 case TransferMode.SAVE:
131                     switch (iDialogType)
132                     {
133                         case DialogTypes.FOLDER:
134                             //TODO: write code for picking a folder for saving
135                             break;
136                         case DialogTypes.FILE:
137                             usedPathPicker = true;
138                             SystemDialog myFilePickerDialog = SystemDialog.createStoreDialog(xMSF);
139                             myFilePickerDialog.callStoreDialog(sDefaultDirectory, sDefaultName, sDefaultFilter);
140                             String sStorePath = myFilePickerDialog.sStorePath;
141                             if (sStorePath != null)
142                             {
143                                 FileAccess myFA = new FileAccess(xMSF);
144                                 xSaveTextBox.setText(myFA.getPath(sStorePath, null));
145                                 sDefaultDirectory = FileAccess.getParentDir(sStorePath);
146                                 sDefaultName = myFA.getFilename(sStorePath);
147                             }
148                             break;
149                         default:
150                             break;
151                     }
152                     break;
153                 case TransferMode.LOAD:
154                     switch (iDialogType)
155                     {
156                         case DialogTypes.FOLDER:
157                             //TODO: write code for picking a folder for loading
158                             break;
159                         case DialogTypes.FILE:
160                             //TODO: write code for picking a file for loading
161                             break;
162                         default:
163                             break;
164                     }
165                     break;
166                 default:
167                     break;
168             }
169         }
170         catch (Exception e)
171         {
172             // TODO Auto-generated catch block
173             e.printStackTrace();
174         }
175     }
176 
callXPathSelectionListener()177     public void callXPathSelectionListener()
178     {
179         if (xAction != null)
180         {
181             xAction.validatePath();
182         }
183     }
184 }
185