package tk.amberide.ide.gui.misc; import tk.amberide.Amber; import tk.amberide.ide.swing.UIUtil; import java.io.InputStream; import java.util.Random; import nu.xom.Builder; import nu.xom.Document; import nu.xom.Elements; /** * * @author Tudor */ public class TipOfTheDay extends javax.swing.JDialog { protected static final String TIPS_MISSING = "That you're missing the tips of the day file?"; protected static final String BASE_DOCUMENT = "<html><body><font size=\"5\">%s</font></html></body>"; protected static Elements TIPS; static { InputStream is = ClassLoader.getSystemResourceAsStream("tips/Tips.xml"); if (is != null) { try { Document tips = new Builder().build(is); TIPS = tips.getRootElement().getChildElements("tip"); } catch (Exception e) { e.printStackTrace(); TIPS = null; } } } /** * Creates new form TipOfTheDay */ public TipOfTheDay(java.awt.Frame parent) { super(parent); initComponents(); tipsPane.setText(randomTip()); } public static boolean showTipOfTheDay() { TipOfTheDay tips = new TipOfTheDay(Amber.getUI()); tips.setVisible(true); return tips.startupCheckBox.isSelected(); } protected static String randomTip() { try { return String.format(BASE_DOCUMENT, TIPS != null ? TIPS.get(new Random().nextInt(TIPS.size())).getValue() : TIPS_MISSING); } catch (Exception e) { return TIPS_MISSING; } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { closeButton = new javax.swing.JButton(); randomTipButton = new javax.swing.JButton(); startupCheckBox = new javax.swing.JCheckBox(); tipsScrollPane = new javax.swing.JScrollPane(); tipsPane = new javax.swing.JTextPane(); questionLabel = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Tip of the Day"); setModal(true); setResizable(false); closeButton.setText("Close"); closeButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { closeButtonActionPerformed(evt); } }); randomTipButton.setText("Random Tip"); randomTipButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { randomTipButtonActionPerformed(evt); } }); startupCheckBox.setSelected(true); startupCheckBox.setText("Show tips on startup"); tipsScrollPane.setBorder(new javax.swing.border.MatteBorder(null)); tipsPane.setEditable(false); tipsPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1)); tipsPane.setContentType("text/html"); // NOI18N tipsPane.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N tipsScrollPane.setViewportView(tipsPane); questionLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N questionLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/Tips.Lightbulb.png.png"))); // NOI18N questionLabel.setText("Did you know...?"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(6, 6, 6) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(questionLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(6, 6, 6)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(layout.createSequentialGroup() .addComponent(startupCheckBox) .addGap(23, 23, 23) .addComponent(randomTipButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(closeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(tipsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 332, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(questionLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(tipsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(10, 10, 10) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(randomTipButton) .addComponent(closeButton) .addComponent(startupCheckBox)) .addGap(10, 10, 10)) ); pack(); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents private void randomTipButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_randomTipButtonActionPerformed tipsPane.setText(randomTip()); }//GEN-LAST:event_randomTipButtonActionPerformed private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed dispose(); }//GEN-LAST:event_closeButtonActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { UIUtil.makeNative(); /* Create and display the dialog */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { TipOfTheDay.showTipOfTheDay(); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton closeButton; private javax.swing.JLabel questionLabel; private javax.swing.JButton randomTipButton; private javax.swing.JCheckBox startupCheckBox; private javax.swing.JTextPane tipsPane; private javax.swing.JScrollPane tipsScrollPane; // End of variables declaration//GEN-END:variables }