001: /*
002: * ProfileListModel.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.profiles;
013:
014: import java.util.ArrayList;
015: import java.util.Collections;
016: import java.util.HashMap;
017: import java.util.Iterator;
018: import java.util.LinkedList;
019: import java.util.List;
020: import java.util.Map;
021: import javax.swing.tree.DefaultMutableTreeNode;
022: import javax.swing.tree.DefaultTreeModel;
023: import javax.swing.tree.MutableTreeNode;
024: import javax.swing.tree.TreeNode;
025: import javax.swing.tree.TreePath;
026:
027: import workbench.db.ConnectionMgr;
028: import workbench.db.ConnectionProfile;
029: import workbench.util.CaseInsensitiveComparator;
030: import workbench.resource.ResourceMgr;
031: import workbench.util.StringUtil;
032:
033: /**
034: *
035: * @author support@sql-workbench.net
036: */
037: class ProfileListModel extends DefaultTreeModel {
038: private DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(
039: "Profiles");
040: private int size;
041:
042: public ProfileListModel() {
043: super (null, true);
044: buildTree();
045: }
046:
047: private void sortList(List<ConnectionProfile> toSort) {
048: if (toSort == null)
049: return;
050: Collections.sort(toSort, ConnectionProfile.getNameComparator());
051: }
052:
053: public void profileChanged(ConnectionProfile profile) {
054: TreePath path = getPath(profile);
055: if (path == null)
056: return;
057: if (path.getPathCount() < 3)
058: return;
059: DefaultMutableTreeNode groupNode = (DefaultMutableTreeNode) path
060: .getPathComponent(2);
061: DefaultMutableTreeNode pNode = (DefaultMutableTreeNode) path
062: .getLastPathComponent();
063: int index = groupNode.getIndex(pNode);
064: fireTreeNodesChanged(this .rootNode, path.getPath(),
065: new int[] { index }, new Object[] { pNode });
066: }
067:
068: public TreePath addProfile(ConnectionProfile profile) {
069: ConnectionMgr conn = ConnectionMgr.getInstance();
070: conn.addProfile(profile);
071: DefaultMutableTreeNode group = findGroupNode(profile.getGroup());
072: DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
073: profile, false);
074: this .insertNodeInto(newNode, group, group.getChildCount());
075: TreePath newPath = new TreePath(new Object[] { this .rootNode,
076: group, newNode });
077: this .size++;
078: return newPath;
079: }
080:
081: public DefaultMutableTreeNode findGroupNode(String group) {
082: if (this .rootNode == null)
083: return null;
084: int children = this .getChildCount(this .rootNode);
085: for (int i = 0; i < children; i++) {
086: DefaultMutableTreeNode n = (DefaultMutableTreeNode) getChild(
087: rootNode, i);
088: if (n == null)
089: continue;
090: String name = (String) n.getUserObject();
091: if (name.equals(group)) {
092: return n;
093: }
094: }
095:
096: return null;
097: }
098:
099: public TreePath[] getGroupNodes() {
100: if (this .rootNode == null)
101: return null;
102: int children = this .getChildCount(this .rootNode);
103: TreePath[] nodes = new TreePath[children];
104: for (int i = 0; i < children; i++) {
105: TreeNode n = (TreeNode) getChild(rootNode, i);
106: if (n == null)
107: continue;
108: nodes[i] = new TreePath(new Object[] { this .rootNode, n });
109: }
110: return nodes;
111: }
112:
113: public List<String> getGroups() {
114: if (this .rootNode == null)
115: return null;
116: List<String> result = new LinkedList<String>();
117: int children = this .getChildCount(this .rootNode);
118: for (int i = 0; i < children; i++) {
119: DefaultMutableTreeNode n = (DefaultMutableTreeNode) getChild(
120: rootNode, i);
121: if (n == null)
122: continue;
123: String group = (String) n.getUserObject();
124: result.add(group);
125: }
126: return result;
127: }
128:
129: public void deleteGroup(String group) {
130: if (group == null)
131: return;
132: DefaultMutableTreeNode node = findGroupNode(group);
133: if (node == null)
134: return;
135: deleteGroupProfiles(node);
136: removeGroupNode(node);
137: }
138:
139: public void deleteGroupProfiles(DefaultMutableTreeNode node) {
140: if (node == null)
141: return;
142: int count = node.getChildCount();
143: if (count == 0)
144: return;
145: ConnectionMgr conn = ConnectionMgr.getInstance();
146: for (int i = 0; i < count; i++) {
147: DefaultMutableTreeNode child = (DefaultMutableTreeNode) node
148: .getChildAt(i);
149: ConnectionProfile prof = (ConnectionProfile) child
150: .getUserObject();
151: conn.removeProfile(prof);
152: }
153: node.removeAllChildren();
154: }
155:
156: public void deleteProfile(ConnectionProfile prof) {
157: TreePath path = getPath(prof);
158: MutableTreeNode node = (MutableTreeNode) path
159: .getLastPathComponent();
160: if (!node.isLeaf())
161: return;
162:
163: ConnectionMgr conn = ConnectionMgr.getInstance();
164: conn.removeProfile(prof);
165: this .removeNodeFromParent(node);
166:
167: this .size--;
168: }
169:
170: public TreePath getFirstProfile() {
171: TreeNode defGroup = this .rootNode.getChildAt(0);
172: Object profile = defGroup.getChildAt(0);
173: return new TreePath(
174: new Object[] { rootNode, defGroup, profile });
175: }
176:
177: public TreePath getPath(ProfileKey def) {
178: if (def == null)
179: return null;
180: ConnectionProfile prof = ConnectionMgr.getInstance()
181: .getProfile(def);
182: if (prof != null) {
183: return getPath(prof);
184: }
185: return null;
186: }
187:
188: public TreePath getPath(ConnectionProfile prof) {
189: if (prof == null)
190: return null;
191: String pGroup = prof.getGroup();
192: Object groupNode = null;
193: if (StringUtil.isEmptyString(pGroup)) {
194: groupNode = this .getChild(this .rootNode, 0);
195: } else {
196: int children = this .getChildCount(this .rootNode);
197: // find the profile group
198: for (int i = 0; i < children; i++) {
199: DefaultMutableTreeNode n = (DefaultMutableTreeNode) getChild(
200: rootNode, i);
201: if (n == null)
202: continue;
203: String g = (String) n.getUserObject();
204: if (pGroup.equals(g)) {
205: groupNode = n;
206: break;
207: }
208: }
209: }
210: if (groupNode == null)
211: return null;
212:
213: int children = this .getChildCount(groupNode);
214: Object profileNode = null;
215: for (int i = 0; i < children; i++) {
216: DefaultMutableTreeNode node = (DefaultMutableTreeNode) this
217: .getChild(groupNode, i);
218: ConnectionProfile p = (ConnectionProfile) node
219: .getUserObject();
220: if (p.equals(prof)) {
221: profileNode = node;
222: }
223: }
224: if (profileNode == null)
225: return null;
226: return new TreePath(new Object[] { rootNode, groupNode,
227: profileNode });
228: }
229:
230: public boolean isChanged() {
231: return ConnectionMgr.getInstance().profilesAreModified();
232: }
233:
234: public int getSize() {
235: return this .size;
236: }
237:
238: public TreePath addGroup(String name) {
239: if (name == null)
240: return null;
241: DefaultMutableTreeNode node = new DefaultMutableTreeNode(name,
242: true);
243: this .insertNodeInto(node, this .rootNode, this .rootNode
244: .getChildCount());
245: return new TreePath(new Object[] { rootNode, node });
246: }
247:
248: public void addEmptyProfile() {
249: ConnectionProfile dummy = new ConnectionProfile();
250: dummy.setName(ResourceMgr.getString("TxtEmptyProfileName"));
251: dummy.setUrl("jdbc:");
252: ConnectionMgr.getInstance().addProfile(dummy);
253: this .size++;
254: buildTree();
255: }
256:
257: public void removeGroupNode(DefaultMutableTreeNode groupNode) {
258: deleteGroupProfiles(groupNode);
259: this .removeNodeFromParent(groupNode);
260: }
261:
262: private void buildTree() {
263: ArrayList<ConnectionProfile> profiles = new ArrayList<ConnectionProfile>(
264: ConnectionMgr.getInstance().getProfiles());
265: if (profiles.size() == 0)
266: return;
267:
268: sortList(profiles);
269:
270: Map<String, List<ConnectionProfile>> groupMap = new HashMap<String, List<ConnectionProfile>>(
271: profiles.size());
272:
273: this .size = profiles.size();
274:
275: for (ConnectionProfile profile : profiles) {
276: String group = profile.getGroup();
277: List<ConnectionProfile> l = groupMap.get(group);
278: if (l == null) {
279: l = new ArrayList<ConnectionProfile>();
280: groupMap.put(group, l);
281: }
282: l.add(profile);
283: }
284:
285: // Make sure the default group is added as the first item!
286: List<String> groups = new ArrayList<String>();
287: groups.addAll(groupMap.keySet());
288: Collections.sort(groups, new CaseInsensitiveComparator());
289:
290: for (String group : groups) {
291: DefaultMutableTreeNode groupNode = new DefaultMutableTreeNode(
292: group, true);
293: rootNode.add(groupNode);
294: List<ConnectionProfile> groupProfiles = groupMap.get(group);
295:
296: this .sortList(groupProfiles);
297: Iterator p = groupProfiles.iterator();
298: while (p.hasNext()) {
299: ConnectionProfile prof = (ConnectionProfile) p.next();
300: DefaultMutableTreeNode profNode = new DefaultMutableTreeNode(
301: prof, false);
302: groupNode.add(profNode);
303: }
304: }
305: this .setRoot(rootNode);
306: }
307:
308: public void moveProfilesToGroup(
309: DefaultMutableTreeNode sourceGroupNode, String newGroup) {
310: DefaultMutableTreeNode target = findGroupNode(newGroup);
311: if (target == null)
312: return;
313: int count = sourceGroupNode.getChildCount();
314: if (count == 0)
315: return;
316: DefaultMutableTreeNode[] nodes = new DefaultMutableTreeNode[count];
317: for (int i = 0; i < count; i++) {
318: nodes[i] = (DefaultMutableTreeNode) sourceGroupNode
319: .getChildAt(i);
320: }
321: moveProfilesToGroup(nodes, target);
322: }
323:
324: public void moveProfilesToGroup(DefaultMutableTreeNode[] profiles,
325: DefaultMutableTreeNode groupNode) {
326: if (profiles == null)
327: return;
328: if (profiles.length == 0)
329: return;
330: if (groupNode == null)
331: return;
332:
333: String groupName = (String) groupNode.getUserObject();
334:
335: for (int i = 0; i < profiles.length; i++) {
336: Object o = profiles[i].getUserObject();
337: ConnectionProfile original = null;
338: if (o instanceof ConnectionProfile) {
339: original = (ConnectionProfile) o;
340: }
341: if (original == null)
342: continue;
343:
344: removeNodeFromParent(profiles[i]);
345: insertNodeInto(profiles[i], groupNode, groupNode
346: .getChildCount());
347: original.setGroup(groupName);
348: }
349: }
350:
351: public void copyProfilesToGroup(DefaultMutableTreeNode[] profiles,
352: DefaultMutableTreeNode groupNode) {
353: if (profiles == null)
354: return;
355: if (profiles.length == 0)
356: return;
357: if (groupNode == null)
358: return;
359:
360: String groupName = (String) groupNode.getUserObject();
361:
362: for (int i = 0; i < profiles.length; i++) {
363: Object o = profiles[i].getUserObject();
364: ConnectionProfile original = null;
365: if (o instanceof ConnectionProfile) {
366: original = (ConnectionProfile) o;
367: }
368: if (original == null)
369: continue;
370:
371: ConnectionProfile copy = original.createCopy();
372: copy.setGroup(groupName);
373: ConnectionMgr.getInstance().addProfile(copy);
374: DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
375: copy, false);
376: insertNodeInto(newNode, groupNode, groupNode
377: .getChildCount());
378: }
379: }
380:
381: }
|