Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ExtractedContentPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-16 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.keywordsearch;
20 
21 import java.awt.ComponentOrientation;
22 import java.awt.EventQueue;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.awt.event.ItemEvent;
26 import java.awt.event.ItemListener;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.concurrent.ExecutionException;
30 import java.util.logging.Level;
31 import javax.swing.JMenuItem;
32 import javax.swing.JTextPane;
33 import javax.swing.SizeRequirements;
34 import javax.swing.SwingWorker;
35 import javax.swing.text.Element;
36 import javax.swing.text.View;
37 import javax.swing.text.ViewFactory;
38 import javax.swing.text.html.HTMLEditorKit;
39 import javax.swing.text.html.HTMLEditorKit.HTMLFactory;
40 import javax.swing.text.html.InlineView;
41 import javax.swing.text.html.ParagraphView;
42 import javax.swing.text.html.StyleSheet;
43 import org.netbeans.api.progress.ProgressHandle;
44 import org.openide.util.NbBundle;
48 
53 class ExtractedContentPanel extends javax.swing.JPanel {
54 
55  private static Logger logger = Logger.getLogger(ExtractedContentPanel.class.getName());
56 
57  ExtractedContentPanel() {
58  initComponents();
59 
60  initControls();
61 
62  customizeComponents();
63 
64  }
65 
66  private void customizeComponents() {
67 
68  HTMLEditorKit editorKit = new HTMLEditorKit() {
69  @Override
70  public ViewFactory getViewFactory() {
71 
72  return new HTMLFactory() {
73  @Override
74  public View create(Element e) {
75  View v = super.create(e);
76  if (v instanceof InlineView) {
77  return new InlineView(e) {
78  @Override
79  public int getBreakWeight(int axis, float pos, float len) {
80  return GoodBreakWeight;
81  }
82 
83  @Override
84  public View breakView(int axis, int p0, float pos, float len) {
85  if (axis == View.X_AXIS) {
86  checkPainter();
87  int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos, len);
88  if (p0 == getStartOffset() && p1 == getEndOffset()) {
89  return this;
90  }
91  return createFragment(p0, p1);
92  }
93  return this;
94  }
95  };
96  } else if (v instanceof ParagraphView) {
97  return new ParagraphView(e) {
98  @Override
99  protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) {
100  if (r == null) {
101  r = new SizeRequirements();
102  }
103  float pref = layoutPool.getPreferredSpan(axis);
104  float min = layoutPool.getMinimumSpan(axis);
105  // Don't include insets, Box.getXXXSpan will include them.
106  r.minimum = (int) min;
107  r.preferred = Math.max(r.minimum, (int) pref);
108  r.maximum = Integer.MAX_VALUE;
109  r.alignment = 0.5f;
110  return r;
111  }
112  };
113  }
114  return v;
115  }
116  };
117  }
118  };
119 
120  // set font size manually in an effort to get fonts in this panel to look
121  // similar to what is in the 'String View' content viewer.
122  StyleSheet ss = editorKit.getStyleSheet();
123  ss.addRule("body {font-size: 8.5px;}"); //NON-NLS
124 
125  extractedTextPane.setEditorKit(editorKit);
126 
127  sourceComboBox.addItemListener(new ItemListener() {
128  @Override
129  public void itemStateChanged(ItemEvent e) {
130  if (e.getStateChange() == ItemEvent.SELECTED) {
131  IndexedText source = (IndexedText) e.getItem();
132  setMarkup(source);
133  }
134  }
135  });
136 
137  setSources(new ArrayList<IndexedText>());
138 
139  extractedTextPane.setComponentPopupMenu(rightClickMenu);
140  ActionListener actList = new ActionListener() {
141  @Override
142  public void actionPerformed(ActionEvent e) {
143  JMenuItem jmi = (JMenuItem) e.getSource();
144  if (jmi.equals(copyMenuItem)) {
145  extractedTextPane.copy();
146  } else if (jmi.equals(selectAllMenuItem)) {
147  extractedTextPane.selectAll();
148  }
149  }
150  };
151  copyMenuItem.addActionListener(actList);
152  selectAllMenuItem.addActionListener(actList);
153  }
154 
160  @SuppressWarnings("unchecked")
161  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
162  private void initComponents() {
163 
164  rightClickMenu = new javax.swing.JPopupMenu();
165  copyMenuItem = new javax.swing.JMenuItem();
166  selectAllMenuItem = new javax.swing.JMenuItem();
167  jScrollPane1 = new javax.swing.JScrollPane();
168  extractedTextPane = new JTextPane(){
169  public boolean getScrollableTracksViewportWidth() {
170  return (getSize().width < 400);
171  }};
172  sourceComboBox = new javax.swing.JComboBox<>();
173  hitLabel = new javax.swing.JLabel();
174  hitCountLabel = new javax.swing.JLabel();
175  hitOfLabel = new javax.swing.JLabel();
176  hitTotalLabel = new javax.swing.JLabel();
177  hitButtonsLabel = new javax.swing.JLabel();
178  hitPreviousButton = new javax.swing.JButton();
179  hitNextButton = new javax.swing.JButton();
180  pageButtonsLabel = new javax.swing.JLabel();
181  pagePreviousButton = new javax.swing.JButton();
182  pageNextButton = new javax.swing.JButton();
183  pagesLabel = new javax.swing.JLabel();
184  pageCurLabel = new javax.swing.JLabel();
185  pageOfLabel = new javax.swing.JLabel();
186  pageTotalLabel = new javax.swing.JLabel();
187 
188  copyMenuItem.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.copyMenuItem.text")); // NOI18N
189  rightClickMenu.add(copyMenuItem);
190 
191  selectAllMenuItem.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.selectAllMenuItem.text")); // NOI18N
192  rightClickMenu.add(selectAllMenuItem);
193 
194  setPreferredSize(new java.awt.Dimension(700, 400));
195 
196  jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
197  jScrollPane1.setPreferredSize(new java.awt.Dimension(700, 400));
198 
199  extractedTextPane.setEditable(false);
200  extractedTextPane.setAutoscrolls(false);
201  extractedTextPane.setMaximumSize(new java.awt.Dimension(2000, 2000));
202  extractedTextPane.setPreferredSize(new java.awt.Dimension(700, 400));
203  jScrollPane1.setViewportView(extractedTextPane);
204 
205  sourceComboBox.setModel(new javax.swing.DefaultComboBoxModel<IndexedText>());
206 
207  hitLabel.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.hitLabel.text")); // NOI18N
208  hitLabel.setToolTipText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.hitLabel.toolTipText")); // NOI18N
209 
210  hitCountLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
211  hitCountLabel.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.hitCountLabel.text")); // NOI18N
212  hitCountLabel.setMaximumSize(new java.awt.Dimension(18, 14));
213  hitCountLabel.setMinimumSize(new java.awt.Dimension(18, 14));
214  hitCountLabel.setPreferredSize(new java.awt.Dimension(18, 14));
215 
216  hitOfLabel.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.hitOfLabel.text")); // NOI18N
217 
218  hitTotalLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
219  hitTotalLabel.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.hitTotalLabel.text")); // NOI18N
220  hitTotalLabel.setMaximumSize(new java.awt.Dimension(18, 14));
221  hitTotalLabel.setMinimumSize(new java.awt.Dimension(18, 14));
222  hitTotalLabel.setPreferredSize(new java.awt.Dimension(18, 14));
223 
224  hitButtonsLabel.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.hitButtonsLabel.text")); // NOI18N
225 
226  hitPreviousButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/btn_step_back.png"))); // NOI18N NON-NLS
227  hitPreviousButton.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.hitPreviousButton.text")); // NOI18N
228  hitPreviousButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
229  hitPreviousButton.setBorderPainted(false);
230  hitPreviousButton.setContentAreaFilled(false);
231  hitPreviousButton.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/btn_step_back_disabled.png"))); // NOI18N NON-NLS
232  hitPreviousButton.setMargin(new java.awt.Insets(2, 0, 2, 0));
233  hitPreviousButton.setPreferredSize(new java.awt.Dimension(23, 23));
234  hitPreviousButton.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/btn_step_back_hover.png"))); // NOI18N NON-NLS
235 
236  hitNextButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/btn_step_forward.png"))); // NOI18N NON-NLS
237  hitNextButton.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.hitNextButton.text")); // NOI18N
238  hitNextButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
239  hitNextButton.setBorderPainted(false);
240  hitNextButton.setContentAreaFilled(false);
241  hitNextButton.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/btn_step_forward_disabled.png"))); // NOI18N NON-NLS
242  hitNextButton.setMargin(new java.awt.Insets(2, 0, 2, 0));
243  hitNextButton.setPreferredSize(new java.awt.Dimension(23, 23));
244  hitNextButton.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/btn_step_forward_hover.png"))); // NOI18N NON-NLS
245 
246  pageButtonsLabel.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.pageButtonsLabel.text")); // NOI18N
247 
248  pagePreviousButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/btn_step_back.png"))); // NOI18N NON-NLS
249  pagePreviousButton.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.pagePreviousButton.text")); // NOI18N
250  pagePreviousButton.setActionCommand(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.pagePreviousButton.actionCommand")); // NOI18N
251  pagePreviousButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
252  pagePreviousButton.setBorderPainted(false);
253  pagePreviousButton.setContentAreaFilled(false);
254  pagePreviousButton.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/btn_step_back_disabled.png"))); // NOI18N NON-NLS
255  pagePreviousButton.setMargin(new java.awt.Insets(2, 0, 2, 0));
256 
257  pageNextButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/btn_step_forward.png"))); // NOI18N NON-NLS
258  pageNextButton.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.pageNextButton.text")); // NOI18N
259  pageNextButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
260  pageNextButton.setBorderPainted(false);
261  pageNextButton.setContentAreaFilled(false);
262  pageNextButton.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/btn_step_forward_disabled.png"))); // NOI18N NON-NLS
263  pageNextButton.setMargin(new java.awt.Insets(2, 0, 2, 0));
264  pageNextButton.setPreferredSize(new java.awt.Dimension(23, 23));
265 
266  pagesLabel.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.pagesLabel.text")); // NOI18N
267 
268  pageCurLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
269  pageCurLabel.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.pageCurLabel.text")); // NOI18N
270 
271  pageOfLabel.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.pageOfLabel.text")); // NOI18N
272 
273  pageTotalLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
274  pageTotalLabel.setText(org.openide.util.NbBundle.getMessage(ExtractedContentPanel.class, "ExtractedContentPanel.pageTotalLabel.text")); // NOI18N
275 
276  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
277  this.setLayout(layout);
278  layout.setHorizontalGroup(
279  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
280  .addGroup(layout.createSequentialGroup()
281  .addContainerGap()
282  .addComponent(hitLabel)
283  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
284  .addComponent(hitCountLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
285  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
286  .addComponent(hitOfLabel)
287  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
288  .addComponent(hitTotalLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
289  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
290  .addComponent(hitButtonsLabel)
291  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
292  .addComponent(hitPreviousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
293  .addGap(0, 0, 0)
294  .addComponent(hitNextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
295  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
296  .addComponent(pagesLabel)
297  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
298  .addComponent(pageCurLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
299  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
300  .addComponent(pageOfLabel)
301  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
302  .addComponent(pageTotalLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
303  .addGap(18, 18, 18)
304  .addComponent(pageButtonsLabel)
305  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
306  .addComponent(pagePreviousButton)
307  .addGap(0, 0, 0)
308  .addComponent(pageNextButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
309  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
310  .addComponent(sourceComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
311  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
312  );
313  layout.setVerticalGroup(
314  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
315  .addGroup(layout.createSequentialGroup()
316  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
317  .addComponent(sourceComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
318  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
319  .addComponent(hitCountLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
320  .addComponent(hitOfLabel)
321  .addComponent(hitTotalLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
322  .addComponent(hitLabel)
323  .addComponent(hitButtonsLabel))
324  .addComponent(hitPreviousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
325  .addComponent(hitNextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
326  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
327  .addComponent(pageButtonsLabel)
328  .addComponent(pageTotalLabel)
329  .addComponent(pagesLabel)
330  .addComponent(pageCurLabel)
331  .addComponent(pageOfLabel))
332  .addComponent(pageNextButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
333  .addComponent(pagePreviousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
334  .addGap(0, 0, 0)
335  .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE))
336  );
337  }// </editor-fold>//GEN-END:initComponents
338  // Variables declaration - do not modify//GEN-BEGIN:variables
339  private javax.swing.JMenuItem copyMenuItem;
340  private javax.swing.JTextPane extractedTextPane;
341  private javax.swing.JLabel hitButtonsLabel;
342  private javax.swing.JLabel hitCountLabel;
343  private javax.swing.JLabel hitLabel;
344  private javax.swing.JButton hitNextButton;
345  private javax.swing.JLabel hitOfLabel;
346  private javax.swing.JButton hitPreviousButton;
347  private javax.swing.JLabel hitTotalLabel;
348  private javax.swing.JScrollPane jScrollPane1;
349  private javax.swing.JLabel pageButtonsLabel;
350  private javax.swing.JLabel pageCurLabel;
351  private javax.swing.JButton pageNextButton;
352  private javax.swing.JLabel pageOfLabel;
353  private javax.swing.JButton pagePreviousButton;
354  private javax.swing.JLabel pageTotalLabel;
355  private javax.swing.JLabel pagesLabel;
356  private javax.swing.JPopupMenu rightClickMenu;
357  private javax.swing.JMenuItem selectAllMenuItem;
358  private javax.swing.JComboBox<IndexedText> sourceComboBox;
359  // End of variables declaration//GEN-END:variables
360 
361  void refreshCurrentMarkup() {
362  IndexedText ms = (IndexedText) sourceComboBox.getSelectedItem();
363  setMarkup(ms);
364  }
365 
372  void setSources(List<IndexedText> sources) {
373  sourceComboBox.removeAllItems();
374  setPanelText(null, false);
375 
376  for (IndexedText ms : sources) {
377  sourceComboBox.addItem(ms);
378  }
379 
380  if (!sources.isEmpty()) {
381  sourceComboBox.setSelectedIndex(0);
382  }
383  }
384 
391  public List<IndexedText> getSources() {
392  ArrayList<IndexedText> sources = new ArrayList<>();
393  for (int i = 0; i < sourceComboBox.getItemCount(); ++i) {
394  sources.add(sourceComboBox.getItemAt(i));
395  }
396  return sources;
397  }
398 
404  public IndexedText getSelectedSource() {
405  return (IndexedText) sourceComboBox.getSelectedItem();
406  }
407 
408  private void setPanelText(String text, boolean detectDirection) {
409  if (text == null) {
410  text = "";
411  }
412 
413  if (detectDirection) {
414  //detect text direction using first 1024 chars and set it
415  //get first up to 1024 chars, strip <pre> tag and unescape html to get the string on which to detect
416  final int len = text.length();
417  final int prefixLen = "<pre>".length(); //NON-NLS
418  if (len > prefixLen) {
419  final int maxOrientChars = Math.min(len, 1024);
420  final String orientDetectText = EscapeUtil.unEscapeHtml(text.substring(prefixLen, maxOrientChars));
421  ComponentOrientation direction = TextUtil.getTextDirection(orientDetectText);
422  //logger.log(Level.INFO, "ORIENTATION LEFT TO RIGHT: " + direction.isLeftToRight());
423  extractedTextPane.applyComponentOrientation(direction);
424  } else {
425  extractedTextPane.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
426  }
427  } else {
428  extractedTextPane.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
429  }
430 
431  extractedTextPane.setText(text);
432  extractedTextPane.setCaretPosition(0);
433  }
434 
435  private void initControls() {
436  hitPreviousButton.setEnabled(false);
437  hitNextButton.setEnabled(false);
438  }
439 
440  void scrollToAnchor(String anchor) {
441  extractedTextPane.scrollToReference(anchor);
442  }
443 
448  void updateCurrentMatchDisplay(int current) {
449  if (current == 0) {
450  hitCountLabel.setText("-");
451  } else {
452  hitCountLabel.setText(Integer.toString(current));
453  }
454  }
455 
460  void updateTotaMatcheslDisplay(int total) {
461  if (total == 0) {
462  hitTotalLabel.setText("-");
463  } else {
464  hitTotalLabel.setText(Integer.toString(total));
465  }
466  }
467 
472  void updateCurrentPageDisplay(int current) {
473  pageCurLabel.setText(Integer.toString(current));
474  }
475 
480  void updateTotalPageslDisplay(int total) {
481  pageTotalLabel.setText(Integer.toString(total));
482  }
483 
484  void resetDisplay() {
485  resetHitDisplay();
486  resetPagesDisplay();
487  }
488 
492  void resetHitDisplay() {
493  hitTotalLabel.setText("-");
494  hitCountLabel.setText("-");
495  }
496 
500  void resetPagesDisplay() {
501  pageCurLabel.setText("-");
502  pageTotalLabel.setText("-");
503  }
504 
510  void enablePrevMatchControl(boolean enable) {
511  hitPreviousButton.setEnabled(enable);
512  }
513 
519  void enableNextMatchControl(boolean enable) {
520  hitNextButton.setEnabled(enable);
521  }
522 
523  void addPrevMatchControlListener(ActionListener l) {
524  hitPreviousButton.addActionListener(l);
525  }
526 
527  void addNextMatchControlListener(ActionListener l) {
528  hitNextButton.addActionListener(l);
529  }
530 
536  void enablePrevPageControl(boolean enable) {
537  pagePreviousButton.setEnabled(enable);
538  }
539 
545  void enableNextPageControl(boolean enable) {
546  pageNextButton.setEnabled(enable);
547  }
548 
549  void addPrevPageControlListener(ActionListener l) {
550  pagePreviousButton.addActionListener(l);
551  }
552 
553  void addNextPageControlListener(ActionListener l) {
554  pageNextButton.addActionListener(l);
555  }
556 
557  void addSourceComboControlListener(ActionListener l) {
558  sourceComboBox.addActionListener(l);
559  }
560 
566  void updateControls(IndexedText source) {
567  updatePageControls(source);
568  updateSearchControls(source);
569  }
570 
576  void updatePageControls(IndexedText source) {
577  if (source == null) {
578  enableNextPageControl(false);
579  enablePrevPageControl(false);
580  updateCurrentPageDisplay(0);
581  updateTotalPageslDisplay(0);
582  return;
583  }
584 
585  updateCurrentPageDisplay(source.getCurrentPage());
586  int totalPages = source.getNumberPages();
587  updateTotalPageslDisplay(totalPages);
588 
589  if (totalPages == 1) {
590  enableNextPageControl(false);
591  enablePrevPageControl(false);
592  } else {
593  if (source.hasNextPage()) {
594  enableNextPageControl(true);
595  } else {
596  enableNextPageControl(false);
597  }
598 
599  if (source.hasPreviousPage()) {
600  enablePrevPageControl(true);
601  } else {
602  enablePrevPageControl(false);
603  }
604  }
605 
606  }
607 
613  void updateSearchControls(IndexedText source) {
614  //setup search controls
615  if (source != null && source.isSearchable()) {
616 
617  updateCurrentMatchDisplay(source.currentItem());
618  updateTotaMatcheslDisplay(source.getNumberHits());
619 
620  if (source.hasNextItem() || source.hasNextPage()) {
621  enableNextMatchControl(true);
622  } else {
623  enableNextMatchControl(false);
624  }
625 
626  if (source.hasPreviousItem() || source.hasPreviousPage()) {
627  enablePrevMatchControl(true);
628  } else {
629  enablePrevMatchControl(false);
630  }
631 
632  } else {
633  enableNextMatchControl(false);
634  enablePrevMatchControl(false);
635  updateCurrentMatchDisplay(0);
636  updateTotaMatcheslDisplay(0);
637  }
638  }
639 
645  private void scrollToCurrentHit(final IndexedText source) {
646  if (source == null || !source.isSearchable()) {
647  return;
648  }
649 
650  //scrolling required invokeLater to enqueue in EDT
651  EventQueue.invokeLater(() -> scrollToAnchor(source.getAnchorPrefix() + source.currentItem()));
652 
653  }
654 
660  private void setMarkup(IndexedText source) {
661  setPanelText(NbBundle.getMessage(this.getClass(), "ExtractedContentPanel.setMarkup.panelTxt"), false);
662  new SetMarkupWorker(source).execute();
663  }
664 
670  private final class SetMarkupWorker extends SwingWorker<String, Void> {
671 
672  private final IndexedText source;
673 
674  private ProgressHandle progress;
675 
676  SetMarkupWorker(IndexedText source) {
677  this.source = source;
678  }
679 
680  @Override
681  protected String doInBackground() throws Exception {
682  progress = ProgressHandle.createHandle(NbBundle.getMessage(this.getClass(), "ExtractedContentPanel.SetMarkup.progress.loading"));
683  progress.setDisplayName(NbBundle.getMessage(this.getClass(), "ExtractedContentPanel.SetMarkup.progress.displayName"));
684  progress.start();
685  progress.switchToIndeterminate();
686 
687  return source.getText();
688  }
689 
690  @NbBundle.Messages({
691  "ExtractedContentPanel.SetMarkup.error=There was an error getting the text for the selected source."})
692  @Override
693  protected void done() {
694  super.done();
695  progress.finish();
696 
697  // see if there are any errors
698  try {
699  String markup = get();
700  if (markup != null) {
701  setPanelText(markup, true);
702  } else {
703  setPanelText("", false);
704  }
705 
706  } catch (InterruptedException | ExecutionException ex) {
707  logger.log(Level.SEVERE, "Error getting marked up text", ex); //NON-NLS
708  setPanelText(Bundle.ExtractedContentPanel_SetMarkup_error(), true);
709  } // catch and ignore if we were cancelled
710  catch (java.util.concurrent.CancellationException ex) {
711  }
712 
713  updateControls(source);
714 
715  scrollToCurrentHit(source);
716  }
717  }
718 }

Copyright © 2012-2016 Basis Technology. Generated on: Mon Jan 2 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.