My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright 2012 stanislawbartkowski@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package databaseobject.util;

import java.net.URI;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.internal.resources.File;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.Status;
import org.eclipse.datatools.connectivity.IConnection;
import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.eclipse.datatools.connectivity.ProfileManager;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import databaseobject.Activator;
import databaseobject.messages.Messages;

/**
* Utility (static) class with different useful methods
*
* @author sbartkowski
*
*/

public class PluginUtil {

private PluginUtil() {

}

/**
* Returns default Shell
*
* @return Shell
*/
static Shell getShell() {
return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
}

/**
* Show message
*
* @param title
* Title of the message window (can be null)
* @param mess
* message content
*/
public static void showMess(String title, String mess) {
MessageDialog.openInformation(getShell(), title, mess);
}

/**
* Display error message (one string)
*
* @param errMessage
* String to display as an error message.
*/
public static void errorMessage(String errMessage) {
Shell parent = getShell();
MessageBox messageBox = new MessageBox(parent, SWT.ICON_ERROR);
messageBox.setMessage(errMessage);
messageBox.open();
}

/**
* Display error message with java exception
*
* @param errMessage
* error message
* @param error
* Exception to display
*/
public static void errorMessage(String errMessage, Throwable error) {
Status sta = new Status(IStatus.ERROR, Activator.PLUGIN_ID, errMessage,
error);
ErrorDialog err = new ErrorDialog(getShell(), null, null, sta,
IStatus.ERROR);
err.open();
}

/**
* Retrieves IResource being selected
*
* @param sel
* ISelection
* @return
*/
public static IResource extractSelection(ISelection sel) {
if (!(sel instanceof IStructuredSelection))
return null;
IStructuredSelection ss = (IStructuredSelection) sel;
Object element = ss.getFirstElement();
if (element instanceof IResource)
return (IResource) element;
if (!(element instanceof IAdaptable))
return null;
IAdaptable adaptable = (IAdaptable) element;
Object adapter = adaptable.getAdapter(IResource.class);
return (IResource) adapter;
}

/**
* Get connection name from project properties
*
* @param iP
* IProject
* @return connection name
*/
public static String getConnectionId(IProject iP) {
try {
String id = iP.getPersistentProperty(new QualifiedName("", //$NON-NLS-1$
CommonProp.DATABASECONNECTION_PROPERTY));
return id;
} catch (CoreException e) {
LogUtil.log(Messages.PluginUtil_1, e);
}
return null;
}

/**
* Get connection from project properties optionally signals that connection
* is not define
*
* @param iP
* IProject
* @param signal
* true: signal error if connection not defined
* @return IConnectionProfile or null if not defined
*/
public static IConnectionProfile getConnection(IProject iP, boolean signal) {
String id = getConnectionId(iP);
if (id == null) {
if (signal) {
errorMessage(Messages.PluginUtil_2);
}
return null;
}
List<IConnectionProfile> pList = getConnectionList();
for (IConnectionProfile p : pList) {
String pId = p.getInstanceID();
if (pId.equals(id)) {
return p;
}
}
return null;
}

/**
* Get list of all DB2 database connection defined in Database tools
*
* @return List of connections
*/
public static List<IConnectionProfile> getConnectionList() {
ProfileManager pManager = ProfileManager.getInstance();
IConnectionProfile[] pI = pManager.getProfiles();
List<IConnectionProfile> pList = new ArrayList<IConnectionProfile>();
for (IConnectionProfile p : pI) {
String pName = p.getProviderId();
// Important: checks if DB2 connection
if (pName.contains(CommonProp.DBType)) {
pList.add(p);
}
}
return pList;
}

private static boolean verifyActionForProject(IProject iP)
throws CoreException {
if (!iP.hasNature(CommonProp.NATURE_ID)) {
errorMessage(Messages.PluginUtil_3);
return false;
}
return true;
}

/**
* Verify if selection is on folder belonging to valid project and proper
* name
*
* @param iP
* IProject
* @param i
* ISelection
* @param folder
* folder name to compare to ('PL/SQL')
* @return true: ok false: not valid and error message was displayed
* @throws CoreException
*/
public static boolean verifyActionForFolder(IProject iP, ISelection i,
String folder) throws CoreException {
if (!verifyActionForProject(iP)) {
return false;
}
IResource iR = extractSelection(i);
if (!(iR instanceof IFolder)) {
errorMessage(Messages.PluginUtil_4);
return false;
}
IFolder f = (IFolder) iR;
IPath pa = f.getFullPath();
String se = pa.lastSegment();
if ((se != null) && !se.equals(folder)) {
errorMessage(Messages.PluginUtil_5 + folder + Messages.PluginUtil_6);
return false;
}
return true;
}

/**
* Verify if selection is on script file
*
* @param iP
* IProject
* @param i
* ISelection
* @return true: ok, false : not valid and error message was displayed
* @throws CoreException
*/
public static boolean verifyActionForDB2File(IProject iP, ISelection i)
throws CoreException {
if (!verifyActionForProject(iP)) {
return false;
}
IResource iR = extractSelection(i);
if (!(iR instanceof IFile)) {
errorMessage(Messages.PluginUtil_7);
return false;
}
IFile f = (IFile) iR;
IPath pa = f.getFullPath();
String se = pa.lastSegment();
if ((se != null) && !se.endsWith(".db2")) { //$NON-NLS-1$
errorMessage(Messages.PluginUtil_9);
return false;
}
return true;
}

/**
* Get connection for project
*
* @param iP
* IProject
* @return Connection (jdbc)
*/
public static Connection getConnection(IProject iP) {
IConnectionProfile iProf = PluginUtil.getConnection(iP, true);
if (iProf == null) {
return null;
}
IStatus s = iProf.connect();
if (!s.isOK()) {
return null;
}
IConnection iC = iProf.createConnection("java.sql.Connection"); //$NON-NLS-1$
if (iC == null) {
return null;
}
Connection jdbc = (Connection) iC.getRawConnection();
return jdbc;
}

/**
* Return full path name for selection
*
* @param arg0
* ISelection
* @return path file name
*/
public static String getFileName(ISelection arg0) {
ITreeSelection iSel = (ITreeSelection) arg0;
TreePath[] t = iSel.getPaths();
TreePath fPath = t[0];
Object o = fPath.getLastSegment();
File f = (File) o;
URI locationURI = f.getLocationURI();
URI u = locationURI;
String fileName = u.getPath();
return fileName;
}
}

Change log

r537 by stanislawbartkowski on Feb 5, 2012   Diff
First commit
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 8383 bytes, 314 lines

File properties

svn:mime-type
text/plain
Powered by Google Project Hosting