1 /*
2  ************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org.  If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 package com.sun.star.wizards.ui;
29 
30 import com.sun.star.awt.XTextComponent;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.uno.Exception;
33 import com.sun.star.wizards.common.FileAccess;
34 import com.sun.star.wizards.common.PropertyNames;
35 import com.sun.star.wizards.common.SystemDialog;
36 
37 public class PathSelection
38 {
39 
40     UnoDialog2 CurUnoDialog;
41     XMultiServiceFactory xMSF;
42     int iDialogType;
43     int iTransferMode;
44     public String sDefaultDirectory = PropertyNames.EMPTY_STRING;
45     public String sDefaultName = PropertyNames.EMPTY_STRING;
46     public String sDefaultFilter = PropertyNames.EMPTY_STRING;
47     public boolean usedPathPicker = false;
48     public XPathSelectionListener xAction;
49     public XTextComponent xSaveTextBox;
50     private final int CMDSELECTPATH = 1;
51     private final int TXTSAVEPATH = 1;
52 
53     public static class DialogTypes
54     {
55 
56         public static final int FOLDER = 0;
57         public static final int FILE = 1;
58     }
59 
60     public static class TransferMode
61     {
62 
63         public static final int SAVE = 0;
64         public static final int LOAD = 1;
65     }
66 
67     public PathSelection(XMultiServiceFactory xMSF, UnoDialog2 CurUnoDialog, int TransferMode, int DialogType)
68     {
69         this.CurUnoDialog = CurUnoDialog;
70         this.xMSF = xMSF;
71         this.iDialogType = DialogType;
72         this.iTransferMode = TransferMode;
73 
74     }
75 
76     public void insert(int DialogStep, int XPos, int YPos, int Width, short CurTabIndex, String LabelText, boolean Enabled, String TxtHelpURL, String BtnHelpURL)
77     {
78 
79         CurUnoDialog.insertControlModel("com.sun.star.awt.UnoControlFixedTextModel", "lblSaveAs", new String[]
80                 {
81                     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
82                 }, new Object[]
83                 {
84                         Boolean.valueOf(Enabled), 8, LabelText, new Integer(XPos), new Integer(YPos), new Integer(DialogStep), new Short(CurTabIndex), new Integer(Width)
85                 });
86 
87         xSaveTextBox = CurUnoDialog.insertTextField("txtSavePath", "callXPathSelectionListener", this, new String[]
88                 {
89                     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
90                 }, new Object[]
91                 {
92                         Boolean.valueOf(Enabled), 12, TxtHelpURL, new Integer(XPos), new Integer(YPos + 10), new Integer(DialogStep), new Short((short) (CurTabIndex + 1)), new Integer(Width - 26)
93                 });
94         //CurUnoDialog.setControlProperty("txtSavePath", PropertyNames.READ_ONLY, Boolean.TRUE);
95         CurUnoDialog.setControlProperty("txtSavePath", PropertyNames.PROPERTY_ENABLED, Boolean.FALSE);
96         CurUnoDialog.insertButton("cmdSelectPath", "triggerPathPicker", this, new String[]
97                 {
98                     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
99                 }, new Object[]
100                 {
101                         Boolean.valueOf(Enabled), 14, BtnHelpURL, "...", new Integer(XPos + Width - 16), new Integer(YPos + 9), new Integer(DialogStep), new Short((short) (CurTabIndex + 2)), 16
102                 });
103 
104     }
105 
106     public void addSelectionListener(XPathSelectionListener xAction)
107     {
108         this.xAction = xAction;
109     }
110 
111     public String getSelectedPath()
112     {
113         return xSaveTextBox.getText();
114     }
115 
116     public void initializePath()
117     {
118         try
119         {
120             FileAccess myFA = new FileAccess(xMSF);
121             xSaveTextBox.setText(myFA.getPath(sDefaultDirectory + "/" + sDefaultName, null));
122         }
123         catch (Exception e)
124         {
125             e.printStackTrace();
126         }
127     }
128 
129     public void triggerPathPicker()
130     {
131         try
132         {
133             switch (iTransferMode)
134             {
135                 case TransferMode.SAVE:
136                     switch (iDialogType)
137                     {
138                         case DialogTypes.FOLDER:
139                             //TODO: write code for picking a folder for saving
140                             break;
141                         case DialogTypes.FILE:
142                             usedPathPicker = true;
143                             SystemDialog myFilePickerDialog = SystemDialog.createStoreDialog(xMSF);
144                             myFilePickerDialog.callStoreDialog(sDefaultDirectory, sDefaultName, sDefaultFilter);
145                             String sStorePath = myFilePickerDialog.sStorePath;
146                             if (sStorePath != null)
147                             {
148                                 FileAccess myFA = new FileAccess(xMSF);
149                                 xSaveTextBox.setText(myFA.getPath(sStorePath, null));
150                                 sDefaultDirectory = FileAccess.getParentDir(sStorePath);
151                                 sDefaultName = myFA.getFilename(sStorePath);
152                             }
153                             break;
154                         default:
155                             break;
156                     }
157                     break;
158                 case TransferMode.LOAD:
159                     switch (iDialogType)
160                     {
161                         case DialogTypes.FOLDER:
162                             //TODO: write code for picking a folder for loading
163                             break;
164                         case DialogTypes.FILE:
165                             //TODO: write code for picking a file for loading
166                             break;
167                         default:
168                             break;
169                     }
170                     break;
171                 default:
172                     break;
173             }
174         }
175         catch (Exception e)
176         {
177             // TODO Auto-generated catch block
178             e.printStackTrace();
179         }
180     }
181 
182     public void callXPathSelectionListener()
183     {
184         if (xAction != null)
185         {
186             xAction.validatePath();
187         }
188     }
189 }
190