| 1 | |
package de.glossmaker.gui.bibtex.main; |
| 2 | |
|
| 3 | |
import javax.swing.JPanel; |
| 4 | |
import java.awt.BorderLayout; |
| 5 | |
import java.awt.event.ActionEvent; |
| 6 | |
import java.awt.event.ActionListener; |
| 7 | |
import java.io.File; |
| 8 | |
|
| 9 | |
import javax.swing.JFileChooser; |
| 10 | |
import javax.swing.JOptionPane; |
| 11 | |
import javax.swing.JScrollPane; |
| 12 | |
import javax.swing.JTable; |
| 13 | |
import javax.swing.JToolBar; |
| 14 | |
import javax.swing.event.ListSelectionEvent; |
| 15 | |
import javax.swing.event.ListSelectionListener; |
| 16 | |
|
| 17 | |
import de.glossmaker.bib.datastructure.ABibItem; |
| 18 | |
import de.glossmaker.bib.datastructure.Article; |
| 19 | |
import de.glossmaker.bib.datastructure.BibItems; |
| 20 | |
import de.glossmaker.bib.datastructure.Book; |
| 21 | |
import de.glossmaker.bib.datastructure.Booklet; |
| 22 | |
import de.glossmaker.bib.datastructure.Conference; |
| 23 | |
import de.glossmaker.bib.datastructure.EBibTeXReference; |
| 24 | |
import de.glossmaker.bib.datastructure.EBookType; |
| 25 | |
import de.glossmaker.bib.datastructure.InBook; |
| 26 | |
import de.glossmaker.bib.datastructure.InCollection; |
| 27 | |
import de.glossmaker.bib.datastructure.InProceedings; |
| 28 | |
import de.glossmaker.bib.datastructure.Manual; |
| 29 | |
import de.glossmaker.bib.datastructure.Mastersthesis; |
| 30 | |
import de.glossmaker.bib.datastructure.Misc; |
| 31 | |
import de.glossmaker.bib.datastructure.Phdthesis; |
| 32 | |
import de.glossmaker.bib.datastructure.Proceedings; |
| 33 | |
import de.glossmaker.bib.datastructure.TechReport; |
| 34 | |
import de.glossmaker.bib.datastructure.Unpublished; |
| 35 | |
import de.glossmaker.bib.undo.UndoBibStack; |
| 36 | |
import de.glossmaker.gloss.datastructure.IItem; |
| 37 | |
import de.glossmaker.gloss.language.Translation; |
| 38 | |
import de.glossmaker.gui.bibtex.BibItemEvent; |
| 39 | |
import de.glossmaker.gui.bibtex.BibItemEvents; |
| 40 | |
import de.glossmaker.gui.bibtex.CustomTableCellEditor; |
| 41 | |
import de.glossmaker.gui.bibtex.EEventCommands; |
| 42 | |
import de.glossmaker.gui.bibtex.EFileOperations; |
| 43 | |
import de.glossmaker.gui.bibtex.EUserInteration; |
| 44 | |
import de.glossmaker.gui.bibtex.EnabledCellRenderer; |
| 45 | |
import de.glossmaker.gui.bibtex.ImageCellRenderer; |
| 46 | |
import de.glossmaker.gui.bibtex.NavigatableTable; |
| 47 | |
import de.glossmaker.gui.bibtex.bars.BibStatusBar; |
| 48 | |
import de.glossmaker.gui.bibtex.bars.BibTexItemsToolBar; |
| 49 | |
import de.glossmaker.gui.bibtex.model.ArticleTableModel; |
| 50 | |
import de.glossmaker.gui.bibtex.model.BibTeXItemsTableModel; |
| 51 | |
import de.glossmaker.gui.bibtex.model.BookTableModel; |
| 52 | |
import de.glossmaker.gui.bibtex.model.BookletTableModel; |
| 53 | |
import de.glossmaker.gui.bibtex.model.CommonConferenceTableModel; |
| 54 | |
import de.glossmaker.gui.bibtex.model.InBookTableModel; |
| 55 | |
import de.glossmaker.gui.bibtex.model.InCollectionTableModel; |
| 56 | |
import de.glossmaker.gui.bibtex.model.ManualTableModel; |
| 57 | |
import de.glossmaker.gui.bibtex.model.MiscTableModel; |
| 58 | |
import de.glossmaker.gui.bibtex.model.ProceedingsTableModel; |
| 59 | |
import de.glossmaker.gui.bibtex.model.TechReportTableModel; |
| 60 | |
import de.glossmaker.gui.bibtex.model.ThesisTableModel; |
| 61 | |
import de.glossmaker.gui.bibtex.model.UnpublishedTableModel; |
| 62 | |
import de.glossmaker.gui.gloss.GlossFileChooser; |
| 63 | |
import de.glossmaker.undo.EUndoCommands; |
| 64 | |
|
| 65 | |
import javax.swing.JSplitPane; |
| 66 | |
|
| 67 | |
import org.bushe.swing.event.EventBus; |
| 68 | |
import org.bushe.swing.event.EventTopicSubscriber; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | 0 | public class BibTeXItemsPanel extends JPanel implements ActionListener, EventTopicSubscriber<BibItemEvents> { |
| 76 | |
private static final long serialVersionUID = -8004441822980602606L; |
| 77 | 0 | private JTable mTable = null; |
| 78 | 0 | private JScrollPane mScrollPane = null; |
| 79 | 0 | private String mFileName = null; |
| 80 | |
private JSplitPane mSplitPane; |
| 81 | |
private BibItems mBibItems; |
| 82 | 0 | private JPanel mTopPanel = null; |
| 83 | 0 | private BibTexItemsToolBar mToolBar = null; |
| 84 | 0 | private NavigatableTable mItemTable = null; |
| 85 | 0 | private BibStatusBar mStatusBar = null; |
| 86 | 0 | private Translation mTranslation = null; |
| 87 | |
|
| 88 | 0 | public BibTeXItemsPanel(String fileName) { |
| 89 | 0 | mFileName = fileName; |
| 90 | 0 | mTranslation = Translation.getInstance(); |
| 91 | 0 | for(EEventCommands commands : EEventCommands.values()) { |
| 92 | 0 | EventBus.subscribe(commands.toString(), this); |
| 93 | |
} |
| 94 | 0 | initialize(); |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
private void initialize() { |
| 98 | 0 | setLayout(new BorderLayout()); |
| 99 | 0 | add(getToolBar(), BorderLayout.NORTH); |
| 100 | 0 | mSplitPane = new JSplitPane(); |
| 101 | 0 | mSplitPane.setResizeWeight(0.5); |
| 102 | 0 | mSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); |
| 103 | 0 | add(mSplitPane, BorderLayout.CENTER); |
| 104 | 0 | mSplitPane.setLeftComponent(getTopPanel()); |
| 105 | 0 | mSplitPane.setRightComponent(new JPanel()); |
| 106 | 0 | add(getStatusBar(), BorderLayout.SOUTH); |
| 107 | 0 | } |
| 108 | |
|
| 109 | |
private JToolBar getToolBar() { |
| 110 | 0 | if (mToolBar == null) { |
| 111 | 0 | mToolBar = new BibTexItemsToolBar(this); |
| 112 | |
} |
| 113 | 0 | return mToolBar; |
| 114 | |
} |
| 115 | |
|
| 116 | |
private JPanel getTopPanel() { |
| 117 | 0 | if (mTopPanel == null) { |
| 118 | 0 | mTopPanel = new JPanel(new BorderLayout()); |
| 119 | 0 | mTopPanel.add(getScrollPane(), BorderLayout.CENTER); |
| 120 | |
} |
| 121 | |
|
| 122 | 0 | return mTopPanel; |
| 123 | |
} |
| 124 | |
|
| 125 | |
private BibStatusBar getStatusBar() { |
| 126 | 0 | if (mStatusBar == null) { |
| 127 | 0 | mStatusBar = new BibStatusBar(); |
| 128 | |
} |
| 129 | 0 | return mStatusBar; |
| 130 | |
} |
| 131 | |
|
| 132 | |
private JScrollPane getScrollPane() { |
| 133 | 0 | if (mScrollPane == null) { |
| 134 | 0 | mScrollPane = new JScrollPane(); |
| 135 | 0 | mScrollPane.setViewportView(getTable()); |
| 136 | |
} |
| 137 | |
|
| 138 | 0 | return mScrollPane; |
| 139 | |
} |
| 140 | |
|
| 141 | |
private void createButtonPanel(IItem item) { |
| 142 | 0 | JScrollPane pane = new JScrollPane(); |
| 143 | 0 | mItemTable = null; |
| 144 | 0 | if (item instanceof Article) { |
| 145 | 0 | mItemTable = new NavigatableTable(new ArticleTableModel(mBibItems, (Article) item)); |
| 146 | 0 | } else if (item instanceof Book) { |
| 147 | 0 | mItemTable = new NavigatableTable(new BookTableModel(mBibItems, (Book) item)); |
| 148 | 0 | } else if (item instanceof Booklet) { |
| 149 | 0 | mItemTable = new NavigatableTable(new BookletTableModel(mBibItems, (Booklet) item)); |
| 150 | 0 | } else if (item instanceof Conference) { |
| 151 | 0 | mItemTable = new NavigatableTable(new CommonConferenceTableModel(mBibItems, (Conference) item)); |
| 152 | 0 | } else if (item instanceof InBook) { |
| 153 | 0 | mItemTable = new NavigatableTable(new InBookTableModel(mBibItems, (InBook) item)); |
| 154 | 0 | } else if (item instanceof InProceedings) { |
| 155 | 0 | mItemTable = new NavigatableTable(new CommonConferenceTableModel(mBibItems, (InProceedings) item)); |
| 156 | 0 | } else if (item instanceof InCollection) { |
| 157 | 0 | mItemTable = new NavigatableTable(new InCollectionTableModel(mBibItems, (InCollection) item)); |
| 158 | 0 | } else if (item instanceof Manual) { |
| 159 | 0 | mItemTable = new NavigatableTable(new ManualTableModel(mBibItems, (Manual) item)); |
| 160 | 0 | } else if (item instanceof Mastersthesis) { |
| 161 | 0 | mItemTable = new NavigatableTable(new ThesisTableModel(mBibItems, (Mastersthesis) item)); |
| 162 | 0 | } else if (item instanceof Phdthesis) { |
| 163 | 0 | mItemTable = new NavigatableTable(new ThesisTableModel(mBibItems, (Phdthesis) item)); |
| 164 | 0 | } else if (item instanceof Misc) { |
| 165 | 0 | mItemTable = new NavigatableTable(new MiscTableModel(mBibItems, (Misc) item)); |
| 166 | 0 | } else if (item instanceof Proceedings) { |
| 167 | 0 | mItemTable = new NavigatableTable(new ProceedingsTableModel(mBibItems, (Proceedings) item)); |
| 168 | 0 | } else if (item instanceof TechReport) { |
| 169 | 0 | mItemTable = new NavigatableTable(new TechReportTableModel(mBibItems, (TechReport) item)); |
| 170 | 0 | } else if (item instanceof Unpublished) { |
| 171 | 0 | mItemTable = new NavigatableTable(new UnpublishedTableModel(mBibItems, (Unpublished) item)); |
| 172 | |
} |
| 173 | |
|
| 174 | 0 | if (mItemTable != null) { |
| 175 | 0 | mItemTable.setName("TABLE_" + ((ABibItem)item).getReference().toUpperCase()); |
| 176 | 0 | mItemTable.setRowHeight(20); |
| 177 | 0 | pane.setViewportView(mItemTable); |
| 178 | 0 | mSplitPane.setRightComponent(pane); |
| 179 | 0 | mItemTable.getColumnModel().getColumn(0).setCellRenderer(new EnabledCellRenderer()); |
| 180 | 0 | mItemTable.getColumnModel().getColumn(1).setCellRenderer(new EnabledCellRenderer()); |
| 181 | 0 | mItemTable.getColumnModel().getColumn(1).setCellEditor(new CustomTableCellEditor()); |
| 182 | |
} |
| 183 | 0 | } |
| 184 | |
|
| 185 | |
private JTable getTable() { |
| 186 | 0 | if (mTable == null) { |
| 187 | 0 | mBibItems = new BibItems(mFileName); |
| 188 | 0 | mTable = new JTable(new BibTeXItemsTableModel(mBibItems)); |
| 189 | 0 | mTable.setName("TABLE_BIBITEMS"); |
| 190 | 0 | mTable.setRowHeight(20); |
| 191 | 0 | mTable.setAutoCreateRowSorter(true); |
| 192 | 0 | mTable.getColumnModel().getColumn(0).setCellRenderer(new ImageCellRenderer()); |
| 193 | 0 | mTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
| 194 | |
|
| 195 | |
@Override |
| 196 | |
public void valueChanged(ListSelectionEvent event) { |
| 197 | 0 | showBibItemTable(); |
| 198 | 0 | } |
| 199 | |
}); |
| 200 | |
|
| 201 | |
} |
| 202 | |
|
| 203 | 0 | return mTable; |
| 204 | |
} |
| 205 | |
|
| 206 | |
private void showBibItemTable() { |
| 207 | 0 | if (mTable.getSelectedRowCount() == 1) { |
| 208 | 0 | int row = mTable.convertRowIndexToModel(mTable.getSelectedRow()); |
| 209 | 0 | IItem item = (IItem) ((BibTeXItemsTableModel)mTable.getModel()).getValueAt(row); |
| 210 | 0 | createButtonPanel(item); |
| 211 | 0 | } else { |
| 212 | 0 | JPanel panel = new JPanel(); |
| 213 | 0 | mSplitPane.setRightComponent(panel); |
| 214 | |
} |
| 215 | 0 | } |
| 216 | |
|
| 217 | |
public void removeAllItems() { |
| 218 | 0 | BibTeXItemsTableModel model = (BibTeXItemsTableModel) mTable.getModel(); |
| 219 | 0 | BibItemEvents events = new BibItemEvents(EEventCommands.REMOVE_SELECTED.toString()); |
| 220 | 0 | for(int i = 0; i < model.getRowCount(); i++) { |
| 221 | 0 | BibItemEvent event = new BibItemEvent(EEventCommands.REMOVE_SELECTED.toString(), |
| 222 | |
(IItem) model.getValueAt(i), null); |
| 223 | 0 | events.add(event); |
| 224 | |
} |
| 225 | 0 | model.removeAll(); |
| 226 | 0 | EventBus.publish(EEventCommands.REMOVE_ALL.toString(), events); |
| 227 | 0 | } |
| 228 | |
|
| 229 | |
public void removeSelectedItems() { |
| 230 | 0 | int rows[] = mTable.getSelectedRows(); |
| 231 | 0 | BibTeXItemsTableModel model = (BibTeXItemsTableModel) mTable.getModel(); |
| 232 | 0 | BibItemEvents events = new BibItemEvents(EEventCommands.REMOVE_SELECTED.toString()); |
| 233 | 0 | for(int i = rows.length - 1; i >= 0; i--) { |
| 234 | 0 | IItem item = model.removeRow(rows[i]); |
| 235 | 0 | BibItemEvent event = new BibItemEvent(EEventCommands.REMOVE_SELECTED.toString(), |
| 236 | |
item, null); |
| 237 | 0 | events.add(event); |
| 238 | |
} |
| 239 | 0 | EventBus.publish(EEventCommands.REMOVE_SELECTED.toString(), events); |
| 240 | 0 | } |
| 241 | |
|
| 242 | |
private void saveAs() { |
| 243 | 0 | int result = JOptionPane.YES_OPTION; |
| 244 | 0 | GlossFileChooser fc = new GlossFileChooser( |
| 245 | |
mTranslation.getValue("filechooser.bib.save_as.title"), |
| 246 | |
mTranslation.getValue("filechooser.bib.description.text"), |
| 247 | |
"bib" |
| 248 | |
); |
| 249 | |
|
| 250 | 0 | if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { |
| 251 | 0 | String fileName = fc.getSelectedFile().getAbsolutePath().toLowerCase(); |
| 252 | 0 | if (fileName.endsWith(".bib") == false) { |
| 253 | 0 | fileName += ".bib"; |
| 254 | |
} |
| 255 | 0 | File f = new File (fileName); |
| 256 | 0 | if (f.exists()) { |
| 257 | 0 | result = JOptionPane.showConfirmDialog(null, mTranslation.getValue("filechooser.override_file.text", fileName)); |
| 258 | |
} |
| 259 | 0 | if (result == JOptionPane.YES_OPTION) { |
| 260 | 0 | mBibItems.setFileName(fileName); |
| 261 | 0 | mBibItems.save(); |
| 262 | |
} |
| 263 | |
} |
| 264 | 0 | } |
| 265 | |
|
| 266 | |
private void saveChanges() { |
| 267 | 0 | if (UndoBibStack.getInstance().undoSize() > 0) { |
| 268 | 0 | int result = JOptionPane.showConfirmDialog(null, |
| 269 | |
mTranslation.getValue("confirmation.save_changes.text"), |
| 270 | |
mTranslation.getValue("confirmation.save_changes.title"), |
| 271 | |
JOptionPane.YES_NO_OPTION); |
| 272 | 0 | if (result == JOptionPane.YES_OPTION) { |
| 273 | 0 | if (mBibItems.getFileName() == null) { |
| 274 | 0 | saveAs(); |
| 275 | |
} else { |
| 276 | 0 | mBibItems.save(); |
| 277 | |
} |
| 278 | |
} |
| 279 | |
} |
| 280 | 0 | } |
| 281 | |
|
| 282 | |
|
| 283 | |
private void openFile() { |
| 284 | 0 | saveChanges(); |
| 285 | 0 | GlossFileChooser fc = new GlossFileChooser( |
| 286 | |
mTranslation.getValue("filechooser.bib.open.title"), |
| 287 | |
mTranslation.getValue("filechooser.bib.description.text"), |
| 288 | |
"bib"); |
| 289 | 0 | if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { |
| 290 | 0 | mBibItems.readFromFile(fc.getSelectedFile().getAbsolutePath()); |
| 291 | 0 | mTable.updateUI(); |
| 292 | 0 | resetUndoStack(); |
| 293 | |
} |
| 294 | 0 | } |
| 295 | |
|
| 296 | |
private void newFile() { |
| 297 | 0 | saveChanges(); |
| 298 | 0 | mBibItems.clear(); |
| 299 | 0 | mBibItems.setFileName(null); |
| 300 | 0 | resetUndoStack(); |
| 301 | 0 | mTable.updateUI(); |
| 302 | 0 | mSplitPane.setRightComponent(new JPanel()); |
| 303 | 0 | } |
| 304 | |
|
| 305 | |
@Override |
| 306 | |
public void actionPerformed(ActionEvent event) { |
| 307 | 0 | ABibItem item = null; |
| 308 | 0 | if (event.getActionCommand().equals(EBibTeXReference.ARTICLE.toString())) { |
| 309 | 0 | item = new Article("", "", "", "", null); |
| 310 | 0 | } else if (event.getActionCommand().equals(EBibTeXReference.BOOK.toString())) { |
| 311 | 0 | item = new Book("", EBookType.AUTHOR, "", "", "", null); |
| 312 | 0 | } else if (event.getActionCommand().equals(EBibTeXReference.BOOKLET.toString())) { |
| 313 | 0 | item = new Booklet("", ""); |
| 314 | 0 | } else if (event.getActionCommand().equals(EBibTeXReference.CONFERENCE.toString())) { |
| 315 | 0 | item = new Conference("", "", "", "", null); |
| 316 | 0 | } else if (event.getActionCommand().equals(EBibTeXReference.INBOOK.toString())) { |
| 317 | 0 | item = new InBook("", EBookType.AUTHOR, "", "", "", "", "", null); |
| 318 | 0 | } else if (event.getActionCommand().equals(EBibTeXReference.INCOLLECTION.toString())) { |
| 319 | 0 | item = new InCollection("", "", "", "", "", null); |
| 320 | 0 | } else if (event.getActionCommand().equals(EBibTeXReference.INPROCEEDINGS.toString())) { |
| 321 | 0 | item = new InProceedings("", "", "", "", null); |
| 322 | 0 | } else if (event.getActionCommand().equals(EBibTeXReference.MANUAL.toString())) { |
| 323 | 0 | item = new Manual("", ""); |
| 324 | 0 | } else if (event.getActionCommand().equals(EBibTeXReference.MASTERSTHESIS.toString())) { |
| 325 | 0 | item = new Mastersthesis("", "", "", "", null); |
| 326 | 0 | } else if (event.getActionCommand().equals(EBibTeXReference.MISC.toString())) { |
| 327 | 0 | item = new Misc(""); |
| 328 | 0 | } else if (event.getActionCommand().equals(EBibTeXReference.PHDTHESIS.toString())) { |
| 329 | 0 | item = new Phdthesis("", "", "", "", null); |
| 330 | 0 | } else if (event.getActionCommand().equals(EBibTeXReference.PROCEEDINGS.toString())) { |
| 331 | 0 | item = new Proceedings("", "", null); |
| 332 | 0 | } else if (event.getActionCommand().equals(EBibTeXReference.TECHREPORT.toString())) { |
| 333 | 0 | item = new TechReport("", "", "", "", null); |
| 334 | 0 | } else if (event.getActionCommand().equals(EBibTeXReference.UNPUBLISHED.toString())) { |
| 335 | 0 | item = new Unpublished("", "", "", ""); |
| 336 | 0 | } else if (event.getActionCommand().equals(EEventCommands.REMOVE_ALL.toString())) { |
| 337 | 0 | removeAllItems(); |
| 338 | 0 | } else if (event.getActionCommand().equals(EEventCommands.REMOVE_SELECTED.toString())) { |
| 339 | 0 | removeSelectedItems(); |
| 340 | 0 | } else if (event.getActionCommand().equals(EUndoCommands.UNDO.toString())) { |
| 341 | 0 | undo(false); |
| 342 | 0 | } else if (event.getActionCommand().equals(EUndoCommands.REDO.toString())) { |
| 343 | 0 | undo(true); |
| 344 | 0 | } else if (event.getActionCommand().equals(EFileOperations.FILE_SAVE.toString())) { |
| 345 | 0 | mBibItems.save(); |
| 346 | 0 | resetUndoStack(); |
| 347 | 0 | } else if (event.getActionCommand().equals(EFileOperations.FILE_SAVE_AS.toString())) { |
| 348 | 0 | saveAs(); |
| 349 | 0 | } else if (event.getActionCommand().equals(EFileOperations.FILE_OPEN.toString())) { |
| 350 | 0 | openFile(); |
| 351 | 0 | } else if (event.getActionCommand().equals(EFileOperations.FILE_NEW.toString())) { |
| 352 | 0 | newFile(); |
| 353 | 0 | } else if (event.getActionCommand().equals(EUserInteration.CLOSE_WINDOW.toString())) { |
| 354 | 0 | EventBus.publish(EUserInteration.CLOSE_WINDOW.toString(), " "); |
| 355 | |
} |
| 356 | |
|
| 357 | |
|
| 358 | 0 | if (item != null) { |
| 359 | 0 | mBibItems.add(item); |
| 360 | 0 | mTable.updateUI(); |
| 361 | 0 | BibItemEvent bibItem = new BibItemEvent(EEventCommands.ADD.toString(), null, item); |
| 362 | 0 | BibItemEvents bibItems = new BibItemEvents(EEventCommands.ADD.toString(), bibItem); |
| 363 | 0 | EventBus.publish(EEventCommands.ADD.toString(), bibItems); |
| 364 | 0 | BibTeXItemsTableModel model = ((BibTeXItemsTableModel) mTable.getModel()); |
| 365 | 0 | int row = model.getRow(item); |
| 366 | 0 | row = mTable.convertRowIndexToView(row); |
| 367 | 0 | mTable.getSelectionModel().setSelectionInterval(row, row); |
| 368 | 0 | if (mBibItems.size() > 0) { |
| 369 | 0 | createButtonPanel(item); |
| 370 | |
} |
| 371 | |
} |
| 372 | 0 | } |
| 373 | |
|
| 374 | |
private void undo(boolean isRedo) { |
| 375 | 0 | BibItemEvents item = null; |
| 376 | 0 | if (isRedo == false) { |
| 377 | 0 | item = UndoBibStack.getInstance().popUndo(); |
| 378 | |
} else { |
| 379 | 0 | item = UndoBibStack.getInstance().popRedo(); |
| 380 | |
} |
| 381 | |
|
| 382 | 0 | if (item != null) { |
| 383 | 0 | if (item.getPropertyName().equals(EEventCommands.CHANGE.toString())) { |
| 384 | 0 | undoChange(item, isRedo); |
| 385 | 0 | } else if (item.getPropertyName().equals(EEventCommands.ADD.toString())) { |
| 386 | 0 | undoAdd(item, isRedo); |
| 387 | 0 | } else if (item.getPropertyName().equals(EEventCommands.REMOVE_ALL.toString()) || |
| 388 | |
item.getPropertyName().equals(EEventCommands.REMOVE_SELECTED.toString())) { |
| 389 | 0 | undoRemove(item, isRedo); |
| 390 | |
} |
| 391 | |
|
| 392 | 0 | if (isRedo == false) { |
| 393 | 0 | UndoBibStack.getInstance().pushRedo(item); |
| 394 | 0 | EventBus.publish(EUndoCommands.UNDO.toString(), item); |
| 395 | |
} else { |
| 396 | 0 | UndoBibStack.getInstance().pushUndo(item); |
| 397 | 0 | EventBus.publish(EUndoCommands.REDO.toString(), item); |
| 398 | |
} |
| 399 | |
|
| 400 | 0 | mTable.updateUI(); |
| 401 | |
} |
| 402 | 0 | } |
| 403 | |
|
| 404 | |
private void updateItemTable() { |
| 405 | 0 | if (mItemTable != null) { |
| 406 | 0 | mItemTable.updateUI(); |
| 407 | 0 | if (mBibItems.size() == 0) { |
| 408 | 0 | mSplitPane.setRightComponent(new JPanel()); |
| 409 | 0 | } else if (mBibItems.size() > 0) { |
| 410 | 0 | mTable.getSelectionModel().setSelectionInterval(0, 0); |
| 411 | |
} |
| 412 | 0 | showBibItemTable(); |
| 413 | |
} |
| 414 | 0 | } |
| 415 | |
|
| 416 | |
private void undoChange(BibItemEvents item, boolean isRedo) { |
| 417 | 0 | ABibItem oldItem = (ABibItem) item.get(0).getOldItem(); |
| 418 | 0 | ABibItem newItem = (ABibItem) item.get(0).getNewItem(); |
| 419 | 0 | ABibItem newItemClone = (ABibItem) item.get(0).getNewItemClone(); |
| 420 | 0 | if (isRedo == false) { |
| 421 | 0 | newItem.setItemValues(oldItem); |
| 422 | |
} else { |
| 423 | 0 | newItem.setItemValues(newItemClone); |
| 424 | |
} |
| 425 | 0 | showBibItemTable(); |
| 426 | 0 | } |
| 427 | |
|
| 428 | |
private void undoAdd(BibItemEvents item, boolean isRedo) { |
| 429 | 0 | IItem newItem = item.get(0).getNewItem(); |
| 430 | 0 | if (isRedo == false) { |
| 431 | 0 | mBibItems.remove(newItem); |
| 432 | |
} else { |
| 433 | 0 | mBibItems.add(newItem); |
| 434 | |
} |
| 435 | 0 | updateItemTable(); |
| 436 | 0 | } |
| 437 | |
|
| 438 | |
private void undoRemove(BibItemEvents item, boolean isRedo) { |
| 439 | 0 | for(int i = 0; i < item.size(); i++) { |
| 440 | 0 | BibItemEvent event = item.get(i); |
| 441 | 0 | if (isRedo == false) { |
| 442 | 0 | mBibItems.add(event.getOldItem()); |
| 443 | |
} else { |
| 444 | 0 | mBibItems.remove(event.getOldItem()); |
| 445 | |
} |
| 446 | |
} |
| 447 | 0 | updateItemTable(); |
| 448 | 0 | } |
| 449 | |
|
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
@Override |
| 454 | |
public void onEvent(String command, BibItemEvents events) { |
| 455 | 0 | boolean updateTable = false; |
| 456 | 0 | if (command.equals(EEventCommands.CHANGE.toString())) { |
| 457 | 0 | BibItemEvent event = events.get(0); |
| 458 | 0 | ABibItem oldItem = (ABibItem) event.getOldItem(); |
| 459 | 0 | ABibItem newItem = (ABibItem) event.getNewItem(); |
| 460 | 0 | updateTable = oldItem.getKey() != null && oldItem.getKey().equals(newItem.getKey()) == false; |
| 461 | 0 | updateTable |= oldItem.getAuthor() != null && oldItem.getAuthor().equals(newItem.getAuthor()) == false; |
| 462 | 0 | updateTable |= oldItem.getTitle() != null && oldItem.getTitle().equals(newItem.getTitle()) == false; |
| 463 | 0 | updateTable |= oldItem.getYear() != newItem.getYear(); |
| 464 | |
} |
| 465 | |
|
| 466 | 0 | if (updateTable) { |
| 467 | 0 | mTable.updateUI(); |
| 468 | |
} |
| 469 | |
|
| 470 | 0 | UndoBibStack.getInstance().pushUndo(events); |
| 471 | 0 | } |
| 472 | |
|
| 473 | |
private void resetUndoStack() { |
| 474 | 0 | UndoBibStack.getInstance().reset(); |
| 475 | |
|
| 476 | 0 | } |
| 477 | |
} |