View Javadoc

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   * Changes a location in the DISC.LOCATION, DISC.COVER_URL, and TRACK.TRACK_URL
38   * fields globally.
39   * <p>
40   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
41   * @author Emil A. Lefkof III <info@melloware.com>
42   * @version 4.0
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      * Constructs a default about dialog using the given owner.
59      * @param owner the dialog's owner
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        // since we are doing a find we want to compact the database on shutdown
67        HibernateUtil.setCompact(true);
68     }
69  
70     /*
71      * (non-Javadoc)
72      * @see com.jgoodies.swing.AbstractDialog#doApply()
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, Resources.getString("messages.Fieldsmusthaveavalue"));
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         	 final String errorMessage = Resources.getString("messages.ErrorUpdatingLocation");
111         	 MessageUtil.showError(this, errorMessage);
112              LOG.error(errorMessage + ex.getMessage(), ex);
113              HibernateUtil.rollbackTransaction();
114          } finally {
115             GuiUtil.setBusyCursor(this, false);
116          }
117       }
118    }
119 
120    /**
121     * Builds and answers the dialog's content.
122     * @return the dialog's content with tabbed pane and button bar
123     */
124    protected JComponent buildContent() {
125       JPanel content = new JPanel(new BorderLayout());
126       content.add(buildPanel(), BorderLayout.NORTH);
127       content.add(buttonBar, BorderLayout.SOUTH);
128       return content;
129    }
130 
131    /**
132     * Builds and returns the dialog's header.
133     * @return the dialog's header component
134     */
135    protected JComponent buildHeader() {
136       return new HeaderPanel(Resources.getString("label.GlobalLocationChange"),
137     	          	         Resources.getString("label.GlobalLocationChangeMessage"),
138                              Resources.LOCATION_TOOL_ICON);
139    }
140 
141    /**
142     * Resizes the given component to give it a quadratic aspect ratio.
143     * @param component the component to be resized
144     */
145    protected void resizeHook(JComponent component) {
146       // Resizer.ONE2ONE.resizeDialogContent(component);
147    }
148 
149    /**
150     * Builds the directory selection panel.
151     * <p>
152     * @return the panel used to select the directory.
153     */
154    private JComponent buildPanel() {
155       JButton[] buttons = new JButton[2];
156       JButton button = createApplyButton();
157       button.setText(Resources.getString("label.Update"));
158       buttonApply = button;
159       buttonClose = createCloseButton(true);
160       buttons[0] = buttonApply;
161       buttons[1] = buttonClose;
162       buttonClose.setText(Resources.getString("label.Close"));
163       buttonBar = ButtonBarFactory.buildRightAlignedBar(buttons);
164       searchField = new JTextField();
165       replaceField = new JTextField();
166       ((JTextField) searchField).setColumns(55);
167       ((JTextField) replaceField).setColumns(55);
168       FormLayout layout = new FormLayout("right:max(14dlu;pref), 4dlu, fill:pref:grow", "p, 4px, p, 4px"); // extra
169                                                                                                             // bottom
170                                                                                                             // space
171                                                                                                             // for
172                                                                                                             // icons
173       PanelBuilder builder = new PanelBuilder(layout);
174       CellConstraints cc = new CellConstraints();
175 
176       builder.addLabel(Resources.getString("label.Search")+": ", cc.xy(1, 1));
177       builder.add(searchField, cc.xy(3, 1));
178       builder.addLabel(Resources.getString("label.Replace")+": ", cc.xy(1, 3));
179       builder.add(replaceField, cc.xy(3, 3));
180 
181       return builder.getPanel();
182    }
183 
184 }