1 package com.melloware.jukes.gui.view.dialogs;
2
3 import java.awt.BorderLayout;
4 import java.awt.Frame;
5 import java.sql.Connection;
6 import java.text.MessageFormat;
7
8 import javax.swing.JButton;
9 import javax.swing.JComponent;
10 import javax.swing.JPanel;
11 import javax.swing.JTextField;
12 import javax.swing.text.JTextComponent;
13
14 import org.apache.commons.lang.StringEscapeUtils;
15 import org.apache.commons.lang.StringUtils;
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19 import com.jgoodies.forms.builder.PanelBuilder;
20 import com.jgoodies.forms.factories.ButtonBarFactory;
21 import com.jgoodies.forms.layout.CellConstraints;
22 import com.jgoodies.forms.layout.FormLayout;
23 import com.jgoodies.uif.AbstractDialog;
24 import com.jgoodies.uif.action.ActionManager;
25 import com.jgoodies.uif.util.ResourceUtils;
26 import com.jgoodies.uifextras.panel.HeaderPanel;
27 import com.melloware.jukes.db.Database;
28 import com.melloware.jukes.db.HibernateUtil;
29 import com.melloware.jukes.exception.InfrastructureException;
30 import com.melloware.jukes.gui.tool.Actions;
31 import com.melloware.jukes.gui.tool.Resources;
32 import com.melloware.jukes.gui.tool.Settings;
33 import com.melloware.jukes.util.GuiUtil;
34 import com.melloware.jukes.util.MessageUtil;
35
36
37
38
39
40
41
42
43
44 public final class LocationChangeDialog extends AbstractDialog {
45
46 private static final Log LOG = LogFactory.getLog(LocationChangeDialog.class);
47 private static final String SQL_DISC_LOCATION = ResourceUtils.getString("sql.update.disc.location");
48 private static final String SQL_DISC_COVER = ResourceUtils.getString("sql.update.disc.cover");
49 private static final String SQL_TRACK_LOCATION = ResourceUtils.getString("sql.update.track.location");
50 private JButton buttonApply;
51 private JButton buttonClose;
52 private JComponent buttonBar;
53 private JTextComponent replaceField;
54 private JTextComponent searchField;
55 private final Settings settings;
56
57
58
59
60
61 public LocationChangeDialog(Frame owner, Settings settings) {
62 super(owner);
63 LOG.debug("Location Changer created.");
64 this.settings = settings;
65 this.settings.getDatabaseLocation();
66
67 HibernateUtil.setCompact(true);
68 }
69
70
71
72
73
74 @SuppressWarnings("deprecation")
75 public void doApply() {
76 LOG.debug("Apply pressed.");
77 if ((StringUtils.isBlank(searchField.getText())) || (StringUtils.isBlank(replaceField.getText()))) {
78 MessageUtil.showwarn(this, "Fields must have a value.");
79 }
80 final String replace = replaceField.getText().trim();
81 final String search = searchField.getText().trim();
82 final String replacesize = String.valueOf(search.length() + 1);
83 final String searchsize = String.valueOf(search.length());
84
85 if (MessageUtil.confirmUpdate(this)) {
86 try {
87 GuiUtil.setBusyCursor(this, true);
88 HibernateUtil.beginTransaction();
89 final Connection connection = HibernateUtil.getSession().connection();
90 String sql = MessageFormat.format(SQL_DISC_LOCATION, new Object[] { StringEscapeUtils.escapeSql(replace),
91 replacesize, searchsize, StringEscapeUtils.escapeSql(search) });
92
93 Database.executeSQL(connection, sql);
94
95 sql = MessageFormat.format(SQL_DISC_COVER, new Object[] { StringEscapeUtils.escapeSql(replace),
96 replacesize, searchsize, StringEscapeUtils.escapeSql(search) });
97
98 Database.executeSQL(connection, sql);
99
100 sql = MessageFormat.format(SQL_TRACK_LOCATION, new Object[] { StringEscapeUtils.escapeSql(replace),
101 replacesize, searchsize, StringEscapeUtils.escapeSql(search) });
102
103 Database.executeSQL(connection, sql);
104
105 HibernateUtil.commitTransaction();
106 ActionManager.get(Actions.CONNECT_ID).actionPerformed(null);
107 ActionManager.get(Actions.REFRESH_ID).actionPerformed(null);
108 MessageUtil.showSuccess(this);
109 } catch (InfrastructureException ex) {
110 LOG.error("Error updating location. " + ex.getMessage(), ex);
111 HibernateUtil.rollbackTransaction();
112 } finally {
113 GuiUtil.setBusyCursor(this, false);
114 }
115 }
116 }
117
118
119
120
121
122 protected JComponent buildContent() {
123 JPanel content = new JPanel(new BorderLayout());
124 content.add(buildPanel(), BorderLayout.NORTH);
125 content.add(buttonBar, BorderLayout.SOUTH);
126 return content;
127 }
128
129
130
131
132
133 protected JComponent buildHeader() {
134 return new HeaderPanel("Global Location Change ",
135 "This dialog allows you to globally change the location of your files. \nJust select a search string"
136 + "such as 'C:\\' and a replace string '\\\\melloware\\'. \n", Resources.LOCATION_TOOL_ICON);
137 }
138
139
140
141
142
143 protected void resizeHook(JComponent component) {
144
145 }
146
147
148
149
150
151
152 private JComponent buildPanel() {
153 JButton[] buttons = new JButton[2];
154 JButton button = createApplyButton();
155 button.setText("Update");
156 buttonApply = button;
157 buttonClose = createCloseButton(true);
158 buttons[0] = buttonApply;
159 buttons[1] = buttonClose;
160 buttonBar = ButtonBarFactory.buildRightAlignedBar(buttons);
161 searchField = new JTextField();
162 replaceField = new JTextField();
163 ((JTextField) searchField).setColumns(55);
164 ((JTextField) replaceField).setColumns(55);
165 FormLayout layout = new FormLayout("right:max(14dlu;pref), 4dlu, fill:pref:grow", "p, 4px, p, 4px");
166
167
168
169
170 PanelBuilder builder = new PanelBuilder(layout);
171 CellConstraints cc = new CellConstraints();
172
173 builder.addLabel("Search: ", cc.xy(1, 1));
174 builder.add(searchField, cc.xy(3, 1));
175 builder.addLabel("Replace: ", cc.xy(1, 3));
176 builder.add(replaceField, cc.xy(3, 3));
177
178 return builder.getPanel();
179 }
180
181 }