Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.72 KB | None | 0 0
  1. package src;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.EventQueue;
  6. import java.awt.Font;
  7.  
  8. import javax.swing.DefaultListCellRenderer;
  9. import javax.swing.DefaultListModel;
  10. import javax.swing.JFrame;
  11. import javax.swing.JList;
  12. import javax.swing.JScrollPane;
  13.  
  14. import com.sun.javafx.css.converters.FontConverter.FontStyleConverter;
  15.  
  16. import src.MainWindow.LogEntry;
  17. import src.MainWindow.LogType;
  18.  
  19. public class MainWindow {
  20.  
  21.     public enum LogType {
  22.         // @formatter:off
  23.         ERROR("Error", Color.RED, Font.BOLD),
  24.         COMMENT("Comment", Color.BLUE, Font.ITALIC),
  25.         READ_INFO("Read_Info", Color.BLACK, 0),
  26.         ACTION("Action", Color.YELLOW, 0),
  27.         PARAM_ERROR("Param_Error", Color.RED, Font.ITALIC | Font.BOLD);
  28.         // @formatter:on
  29.  
  30.         String text;
  31.         Color color;
  32.         int fontStyle;
  33.  
  34.         private LogType(String s, Color color, int fontStyle) {
  35.             this.text = s;
  36.             this.color = color;
  37.             this.fontStyle = fontStyle;
  38.         }
  39.  
  40.         public void setText(String s) {
  41.             this.text = s;
  42.         }
  43.  
  44.         public String getText() {
  45.             return text;
  46.         }
  47.  
  48.         public int getFontStyle() {
  49.             return fontStyle;
  50.         }
  51.  
  52.         public Color getColor() {
  53.             return color;
  54.         }
  55.     };
  56.  
  57.     public static class LogEntry {
  58.         private LogType type;
  59.         private String text;
  60.  
  61.         public LogEntry(LogType type, String text) {
  62.             this.type = type;
  63.             this.text = text;
  64.         }
  65.  
  66.         public LogType getType() {
  67.             return type;
  68.         }
  69.  
  70.         public String getText() {
  71.             return text;
  72.         }
  73.     }
  74.  
  75.     private JFrame frmNavdng;
  76.     private static DefaultListModel<LogEntry> logListModel = new DefaultListModel<>();
  77.     private static JList<LogEntry> log_List = new JList<>(logListModel);
  78.  
  79.     public MainWindow() {
  80.         initialize();
  81.     }
  82.  
  83.     public static void add_Log_Line(LogType type, String s) {
  84.         // add action to the log list
  85.         type.setText(s);
  86.         logListModel.addElement(new LogEntry(type, s));
  87.         log_List.ensureIndexIsVisible(log_List.getLastVisibleIndex());
  88.     }
  89.  
  90.     private void initialize() {
  91.         /** Frame Initialisation */
  92.         frmNavdng = new JFrame();
  93.         frmNavdng.setFont(new Font("Tahoma", Font.PLAIN, 12));
  94.         frmNavdng.setTitle("list_window");
  95.         frmNavdng.setResizable(false);
  96.         frmNavdng.setBounds(100, 100, 640, 400);
  97.         frmNavdng.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  98.         frmNavdng.getContentPane().setLayout(null);
  99.  
  100.         JScrollPane scrollPane = new JScrollPane();
  101.         scrollPane.setBounds(0, 0, frmNavdng.getWidth(), frmNavdng.getHeight());
  102.         frmNavdng.getContentPane().add(scrollPane);
  103.         scrollPane.setViewportView(log_List);
  104.         log_List.setToolTipText("history of the last actions");
  105.         log_List.setVisibleRowCount(10);
  106.         log_List.setValueIsAdjusting(true);
  107.         log_List.setFont(new Font("Tahoma", Font.PLAIN, 10));
  108.         log_List.setCellRenderer(new MyCellRenderer());
  109.     }
  110.  
  111.     public static void main(String[] args) {
  112.         EventQueue.invokeLater(new Runnable() {
  113.             public void run() {
  114.                 try {
  115.                     MainWindow window = new MainWindow();
  116.                     window.frmNavdng.setVisible(true);
  117.                     LogType[] values = LogType.values();
  118.                     for (LogType logType : values) {
  119.                         add_Log_Line(logType, "This is log type " + logType.getText());
  120.                     }
  121.                 } catch (Exception e) {
  122.                     e.toString();
  123.                 }
  124.             }
  125.         });
  126.     }
  127.  
  128. }
  129.  
  130. class MyCellRenderer extends DefaultListCellRenderer {
  131.  
  132.     private static final long serialVersionUID = 1L;
  133.  
  134.     public MyCellRenderer() {
  135.         setOpaque(true);
  136.     }
  137.  
  138.     @Override public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
  139.             boolean cellHasFocus) {
  140.         LogEntry entry = (LogEntry) value;
  141.  
  142.         value = entry.getText();
  143.  
  144.         Color background = Color.white;
  145.         Color foreground = entry.getType().getColor();
  146.  
  147.         Font font = list.getFont();
  148.  
  149.         font = new Font(list.getFont().getFontName(), entry.getType().getFontStyle(), list.getFont().getSize());
  150.  
  151.         if (isSelected)
  152.             background = Color.lightGray;
  153.  
  154.         Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
  155.  
  156.         c.setForeground(foreground);
  157.         c.setBackground(background);
  158.         c.setFont(font);
  159.  
  160.         return c;
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement