Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
TextPrompt.java
Go to the documentation of this file.
1 package org.sleuthkit.autopsy.corecomponents;
2 
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.border.*;
7 import javax.swing.event.*;
8 import javax.swing.text.*;
9 
20 public final class TextPrompt extends JLabel
21  implements FocusListener, DocumentListener {
22 
23  public enum Show {
24 
28  }
29 
30  private JTextComponent component;
31  private Document document;
32 
33  private Show show;
34  private boolean showPromptOnce;
35  private int focusLost;
36 
37  public TextPrompt(String text, JTextComponent component) {
38  this(text, component, Show.ALWAYS);
39  }
40 
41  public TextPrompt(String text, JTextComponent component, Show show) {
42  this.component = component;
43  setShow(show);
44  document = component.getDocument();
45 
46  setText(text);
47  setFont(component.getFont());
48  setForeground(component.getForeground());
49  setBorder(new EmptyBorder(component.getInsets()));
50  setHorizontalAlignment(JLabel.LEADING);
51 
52  component.addFocusListener(this);
53  document.addDocumentListener(this);
54 
55  component.setLayout(new BorderLayout());
56  component.add(this);
58  }
59 
66  public void changeAlpha(float alpha) {
67  changeAlpha((int) (alpha * 255));
68  }
69 
76  public void changeAlpha(int alpha) {
77  alpha = alpha > 255 ? 255 : alpha < 0 ? 0 : alpha;
78 
79  Color foreground = getForeground();
80  int red = foreground.getRed();
81  int green = foreground.getGreen();
82  int blue = foreground.getBlue();
83 
84  Color withAlpha = new Color(red, green, blue, alpha);
85  super.setForeground(withAlpha);
86  }
87 
95  public void changeStyle(int style) {
96  setFont(getFont().deriveFont(style));
97  }
98 
104  public Show getShow() {
105  return show;
106  }
107 
119  public void setShow(Show show) {
120  this.show = show;
121  }
122 
128  public boolean getShowPromptOnce() {
129  return showPromptOnce;
130  }
131 
139  public void setShowPromptOnce(boolean showPromptOnce) {
140  this.showPromptOnce = showPromptOnce;
141  }
142 
147  private void checkForPrompt() {
148  // Text has been entered, remove the prompt
149 
150  if (document.getLength() > 0) {
151  setVisible(false);
152  return;
153  }
154 
155  // Prompt has already been shown once, remove it
156  if (showPromptOnce && focusLost > 0) {
157  setVisible(false);
158  return;
159  }
160 
161  // Check the Show property and component focus to determine if the
162  // prompt should be displayed.
163  if (component.hasFocus()) {
164  if (show == Show.ALWAYS
165  || show == Show.FOCUS_GAINED) {
166  setVisible(true);
167  } else {
168  setVisible(false);
169  }
170  } else {
171  if (show == Show.ALWAYS
172  || show == Show.FOCUS_LOST) {
173  setVisible(true);
174  } else {
175  setVisible(false);
176  }
177  }
178  }
179 
180 // Implement FocusListener
181  @Override
182  public void focusGained(FocusEvent e) {
183  checkForPrompt();
184  }
185 
186  @Override
187  public void focusLost(FocusEvent e) {
188  focusLost++;
189  checkForPrompt();
190  }
191 
192 // Implement DocumentListener
193  @Override
194  public void insertUpdate(DocumentEvent e) {
195  checkForPrompt();
196  }
197 
198  @Override
199  public void removeUpdate(DocumentEvent e) {
200  checkForPrompt();
201  }
202 
203  @Override
204  public void changedUpdate(DocumentEvent e) {
205  }
206 }
TextPrompt(String text, JTextComponent component, Show show)
Definition: TextPrompt.java:41
TextPrompt(String text, JTextComponent component)
Definition: TextPrompt.java:37
void setShowPromptOnce(boolean showPromptOnce)

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