示例#1
0
  private void initStyles() {
    sc = new StyleContext();
    Style parent = sc.getStyle(StyleContext.DEFAULT_STYLE);

    StyleConstants.setFontFamily(parent, "courier");
    StyleConstants.setFontSize(parent, 13);

    styleElementName = sc.addStyle("elementName", parent);
    StyleConstants.setForeground(styleElementName, new Color(128, 0, 0));

    styleAttribtuteName = sc.addStyle("attributeName", parent);
    StyleConstants.setForeground(styleAttribtuteName, Color.RED);

    styleAttribtuteValue = sc.addStyle("attributeValue", parent);

    styleContent = sc.addStyle("content", parent);
    StyleConstants.setBackground(styleContent, new Color(200, 255, 100));

    styleOperator = sc.addStyle("operator", parent);
    StyleConstants.setForeground(styleOperator, Color.BLUE);
    StyleConstants.setBold(styleOperator, true);

    styleComments = sc.addStyle("comments", parent);
    StyleConstants.setForeground(styleComments, new Color(128, 128, 128)); // Hooker's green

    styleCData = sc.addStyle("cdata", parent);
    StyleConstants.setForeground(styleCData, new Color(30, 30, 0));
    StyleConstants.setBackground(styleCData, new Color(250, 250, 240));

    styleProcessingInstructions = sc.addStyle("processingIntruction", parent);
    styleDOCTYPE = sc.addStyle("doctype", styleComments);
  }
  public ConsoleWindow() {
    if (gui) {
      window = new JFrame("Package Tracking Console");
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      window.setSize((int) (screenSize.getWidth() / 4), (int) (screenSize.getHeight() / 1.5));
      window.setLocationRelativeTo(null);
      window.getContentPane().setLayout(new BorderLayout());

      menuBar = new JMenuBar();
      menuBar.add("test", new JLabel("Testing"));
      window.setJMenuBar(menuBar);

      StyleContext sc = new StyleContext();
      document = new DefaultStyledDocument(sc);

      // Styles

      send = sc.addStyle("(Send)", null);
      send.addAttribute(StyleConstants.Foreground, Color.CYAN);

      info = sc.addStyle("(Info)", null);
      info.addAttribute(StyleConstants.Foreground, Color.WHITE);

      warning = sc.addStyle("(Warning)", null);
      warning.addAttribute(StyleConstants.Foreground, Color.YELLOW);

      error = sc.addStyle("(Error)", null);
      error.addAttribute(StyleConstants.Foreground, Color.RED);
      error.addAttribute(StyleConstants.Bold, new Boolean(true));

      textArea = new JTextPane(document);
      textArea.setBackground(new Color(50, 50, 50));
      textArea.setEditable(true);
      textArea.setBorder(null);
      textArea.setForeground(Color.WHITE);

      scrollPane = new JScrollPane(textArea);
      new SmartScroller(scrollPane);

      window.getContentPane().add(scrollPane);

      input = new JTextField();
      input.setBackground(new Color(50, 50, 50));
      input.setForeground(Color.WHITE);
      input.setCaretColor(Color.WHITE);
      input.addActionListener(new test());
      window.getContentPane().add(input, BorderLayout.SOUTH);

      window.setVisible(true);
      info.addAttribute(StyleConstants.Foreground, Color.BLUE);
    }
  }
  @SuppressWarnings({"LeakingThisInConstructor", "BooleanConstructorCall"})
  public ConversationTopComponent() {
    initComponents();
    //		setName(Bundle.CTL_ConversationTopComponent());
    //		setToolTipText(Bundle.HINT_ConversationTopComponent());
    m_commandHistory = new Vector<String>();

    StyleContext sc = new StyleContext();
    commanddoc = new DefaultStyledDocument(sc);

    responseStyle = sc.addStyle("Response", null);
    responseStyle.addAttribute(StyleConstants.Foreground, Color.black);
    responseStyle.addAttribute(StyleConstants.FontSize, 16);
    responseStyle.addAttribute(StyleConstants.FontFamily, "serif");
    responseStyle.addAttribute(StyleConstants.Bold, false);

    errorStyle = sc.addStyle("Error", null);
    errorStyle.addAttribute(StyleConstants.Foreground, Color.red);
    errorStyle.addAttribute(StyleConstants.FontSize, 16);
    errorStyle.addAttribute(StyleConstants.FontFamily, "serif");
    errorStyle.addAttribute(StyleConstants.Bold, true);

    commandStyle = sc.addStyle("Command", null);
    commandStyle.addAttribute(StyleConstants.Foreground, Color.blue);
    commandStyle.addAttribute(StyleConstants.FontSize, 16);
    commandStyle.addAttribute(StyleConstants.FontFamily, "serif");
    commandStyle.addAttribute(StyleConstants.Bold, false);

    inCommandStyle = sc.addStyle("InCommand", null);
    inCommandStyle.addAttribute(StyleConstants.Foreground, Color.green);
    inCommandStyle.addAttribute(StyleConstants.FontSize, 16);
    inCommandStyle.addAttribute(StyleConstants.FontFamily, "serif");
    inCommandStyle.addAttribute(StyleConstants.Bold, false);

    warningStyle = sc.addStyle("Warning", null);
    warningStyle.addAttribute(StyleConstants.Foreground, Color.yellow);
    warningStyle.addAttribute(StyleConstants.FontSize, 16);
    warningStyle.addAttribute(StyleConstants.FontFamily, "serif");
    warningStyle.addAttribute(StyleConstants.Bold, true);

    textCommand.setDocument(commanddoc);

    try {
      commanddoc.insertString(0, "Pronto!\n\n", responseStyle);
    } catch (BadLocationException ex) {
      Exceptions.printStackTrace(ex);
    }
  }
示例#4
0
public class FBChatTab extends JPanel {

  private static final long serialVersionUID = 1L;
  private int lastEnd = 0;

  public final ChatWindowClient cwc;
  public final FBChatTab myself = this;
  public boolean logged_in = false;

  public JPanel tabPanel;
  public JTextField textChat = new JTextField();
  public JList<Object> onlineList = new JList<Object>(new DefaultListModel<Object>());
  public JList<Object> offlineList = new JList<Object>(new DefaultListModel<Object>());
  public JLabel onlineLabel = new JLabel("\u5728\u7DDA\u5217\u8868");

  public StyleContext sc = new StyleContext();
  Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
  final Style mainStyle = sc.addStyle(null, defaultStyle);
  final Style boldStyle = sc.addStyle(null, defaultStyle);
  final Style grayStyle = sc.addStyle(null, defaultStyle);
  final Style boldGrayStyle = sc.addStyle(null, defaultStyle);

  public JTextPane textPane = new JTextPane(new DefaultStyledDocument(sc));
  public JScrollPane textScroll = new JScrollPane();
  public JButton btnLeavePage = new JButton("\u96E2\u958B");

  public JLabel lblfacebook = new JLabel("\u2190 \u767B\u5165Facebook");
  public JLabel fb_label = new JLabel("");
  public JLabel usernameLabel = new JLabel("");
  public JLabel profilePicLabel = new JLabel("");

  public FBChatTab(ChatWindowClient c) {
    this.cwc = c;

    tabPanel = new JPanel();
    tabPanel.setBounds(100, 100, 903, 604);
    tabPanel.setLayout(null);

    StyleConstants.setBold(boldStyle, true);
    StyleConstants.setBold(boldGrayStyle, true);
    StyleConstants.setForeground(mainStyle, Color.BLACK);
    StyleConstants.setForeground(boldStyle, Color.BLACK);
    StyleConstants.setForeground(grayStyle, Color.GRAY);
    StyleConstants.setForeground(boldGrayStyle, Color.DARK_GRAY);
    textPane.addStyle("NormalMessage", mainStyle);
    textPane.addStyle("UserName", boldStyle);
    textPane.addStyle("SystemMessage", grayStyle);
    textPane.addStyle("FriendName", boldGrayStyle);
    textPane.setBorder(null);
    onlineLabel.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mousePressed(MouseEvent arg0) {
            cwc.fbClient.updateUserList();
          }
        });
    onlineLabel.setToolTipText("\u9EDE\u64CA\u5373\u53EF\u5237\u65B0\u5217\u8868");

    onlineLabel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
    onlineLabel.setBounds(11, 10, 150, 25);
    onlineLabel.setHorizontalAlignment(SwingConstants.CENTER);
    tabPanel.add(onlineLabel);

    btnLeavePage.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            cwc.webBrowser.navigate("about:blank");
            cwc.fbClient.clear();
            cwc.initFbBrowser();
            cwc.tabbedPane.remove(tabPanel);
            tabPanel.setName(null);
            clearText();
            textChat.setEditable(false);
            lblfacebook.setVisible(true);
            fb_label.setVisible(true);
            profilePicLabel.setVisible(false);
            usernameLabel.setVisible(false);
          }
        });
    btnLeavePage.setBounds(74, 546, 87, 25);
    tabPanel.add(btnLeavePage);

    textChat.setBounds(172, 546, 822, 25);
    textChat.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            String message = textChat.getText();
            printText(message, cwc.fbClient.getUserName());
            try {
              cwc.fbClient.getConversation().sendMessage(message);
            } catch (XMPPException e) {
              System.out.println("Message sending error!");
            }
            textChat.setText(null);
          }
        });
    tabPanel.add(textChat);
    textChat.setEditable(false);

    textPane.setEditable(false);
    textScroll.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
    textScroll.setBounds(172, 10, 822, 526);
    tabPanel.add(textScroll);
    textScroll.setViewportView(textPane);

    fb_label.addMouseListener(
        new MouseAdapter() {
          public void mousePressed(MouseEvent arg0) {
            if (logged_in) return;
            NativeInterface.open();
            cwc.webBrowser.navigate(
                "https://www.facebook.com/dialog/oauth?scope=xmpp_login&redirect_uri=https://www.facebook.com/connect/login_success.html&display=popup&response_type=token&client_id=284623318334487");
            new Thread(new FBChatClient(myself)).start();
          }
        });
    fb_label.setToolTipText("\u767B\u5165Facebook");
    fb_label.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
    fb_label.setBounds(11, 544, 50, 50);
    BufferedImage img_scaled = null, img = null;
    try {
      img_scaled = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
      img =
          ImageIO.read(
              new URL(
                  "http://profile.ak.fbcdn.net/hprofile-ak-snc6/c13.12.160.160/281468_259870444030331_5568518_n.jpg"));
    } catch (IOException e) {
      e.printStackTrace();
    }
    img_scaled.createGraphics().drawImage(img, 0, 0, 50, 50, null);
    fb_label.setIcon(new ImageIcon(img_scaled));
    tabPanel.add(fb_label);

    lblfacebook.setBounds(64, 579, 150, 15);
    tabPanel.add(lblfacebook);

    usernameLabel.setBounds(64, 579, 150, 15);
    usernameLabel.setVisible(false);
    tabPanel.add(usernameLabel);

    profilePicLabel.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
    profilePicLabel.setBounds(11, 544, 50, 50);
    profilePicLabel.setVisible(false);
    tabPanel.add(profilePicLabel);

    JScrollPane onlineListScrollPane = new JScrollPane();
    onlineListScrollPane.setBounds(11, 45, 150, 220);
    tabPanel.add(onlineListScrollPane);
    onlineListScrollPane.setViewportView(onlineList);

    onlineList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    onlineList.setLayoutOrientation(JList.VERTICAL);
    onlineList.setFixedCellHeight(25);
    onlineList.addMouseListener(
        new MouseAdapter() {
          @SuppressWarnings("unchecked")
          public void mouseClicked(MouseEvent evt) {
            // when double clicked on a friend name
            JList<FBUser> list = (JList<FBUser>) evt.getSource();
            if (evt.getClickCount() == 2) {
              if (list.getSelectedValue() == null) return;
              String id = list.getSelectedValue().getId();
              if (id == null) return;

              clearText();
              printSystemMsg(
                  "<\u7cfb\u7d71\u8a0a\u606f> \u5df2\u958b\u555f\u8207"
                      + cwc.fbClient.getName(id)
                      + "\u7684\u5c0d\u8a71\u3002\n");
              textChat.setEditable(true);
              textChat.requestFocus();
              cwc.fbClient.openConversation(id);
            }
            offlineList.clearSelection();
          }
        });
    onlineList.setCellRenderer(new myCellRenderer());

    JScrollPane offlineListScrollPane = new JScrollPane();
    offlineListScrollPane.setBounds(11, 315, 150, 220);
    tabPanel.add(offlineListScrollPane);
    offlineListScrollPane.setViewportView(offlineList);

    offlineList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    offlineList.setLayoutOrientation(JList.VERTICAL);
    offlineList.setFixedCellHeight(25);
    offlineList.addMouseListener(
        new MouseAdapter() {
          @SuppressWarnings("unchecked")
          public void mouseClicked(MouseEvent evt) {
            // when double clicked on a friend name
            JList<FBUser> list = (JList<FBUser>) evt.getSource();
            if (evt.getClickCount() == 2) {
              if (list.getSelectedValue() == null) return;
              String id = list.getSelectedValue().getId();
              if (id == null) return;

              clearText();
              printSystemMsg(
                  "<\u7cfb\u7d71\u8a0a\u606f> \u5df2\u958b\u555f\u8207"
                      + cwc.fbClient.getName(id)
                      + "\u7684\u5c0d\u8a71\u3002\n");
              textChat.setEditable(true);
              textChat.requestFocus();
              cwc.fbClient.openConversation(id);
            }
            onlineList.clearSelection();
          }
        });
    offlineList.setCellRenderer(new myCellRenderer());

    JLabel offlineLabel = new JLabel("\u96E2\u7DDA\u5217\u8868");
    offlineLabel.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mousePressed(MouseEvent arg0) {
            cwc.fbClient.updateUserList();
          }
        });
    offlineLabel.setToolTipText("\u9EDE\u64CA\u5373\u53EF\u5237\u65B0\u5217\u8868");
    offlineLabel.setHorizontalAlignment(SwingConstants.CENTER);
    offlineLabel.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
    offlineLabel.setBounds(11, 280, 150, 25);
    tabPanel.add(offlineLabel);
  }

  public void printInputMsg(String name) {
    textPane.setEditable(true);
    if (lastEnd >= 0) {
      textPane.setSelectionStart(lastEnd);
      lastEnd = -1;
    } else textPane.setSelectionStart(textPane.getText().length());
    lastEnd = textPane.getSelectionStart();
    textPane.setSelectionEnd(textPane.getText().length());
    textPane.replaceSelection("");

    textPane.setSelectionStart(textPane.getText().length());
    textPane.setSelectionEnd(textPane.getText().length());
    textPane.setCharacterAttributes(textPane.getStyle("SystemMessage"), true);
    textPane.replaceSelection(name + "\u6b63\u5728\u8f38\u5165\u8a0a\u606f..");
    textPane.setEditable(false);
  }

  public void printSystemMsg(String s) {
    textPane.setEditable(true);
    if (lastEnd >= 0) {
      textPane.setSelectionStart(lastEnd);
      lastEnd = -1;
    } else textPane.setSelectionStart(textPane.getText().length());
    textPane.setSelectionEnd(textPane.getText().length());
    textPane.replaceSelection("");

    textPane.setSelectionStart(textPane.getText().length());
    textPane.setSelectionEnd(textPane.getText().length());
    textPane.setCharacterAttributes(textPane.getStyle("SystemMessage"), true);
    textPane.replaceSelection(s);
    textPane.setEditable(false);
  }

  public void printText(String s, String username) {
    textPane.setEditable(true);
    if (lastEnd >= 0) {
      textPane.setSelectionStart(lastEnd);
      lastEnd = -1;
    } else textPane.setSelectionStart(textPane.getText().length());
    textPane.setSelectionEnd(textPane.getText().length());
    textPane.replaceSelection("");

    textPane.setSelectionStart(textPane.getText().length());
    textPane.setSelectionEnd(textPane.getText().length());
    if (username.equals(cwc.fbClient.getUserName()))
      textPane.setCharacterAttributes(textPane.getStyle("UserName"), true);
    else textPane.setCharacterAttributes(textPane.getStyle("FriendName"), true);
    textPane.replaceSelection(username + ":\n");

    textPane.setSelectionStart(textPane.getText().length());
    textPane.setSelectionEnd(textPane.getText().length());
    textPane.setCharacterAttributes(textPane.getStyle("NormalMessage"), true);
    textPane.replaceSelection(" \u2027 " + s + "\n");
    textPane.setEditable(false);
  }

  public void clearText() {
    textPane.setEditable(true);
    textPane.setText("");
    textPane.setEditable(false);
  }

  public void setProfilePic(String url) {
    BufferedImage img_scaled = null, img = null;
    try {
      img_scaled = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
      img = ImageIO.read(new URL(url));
    } catch (IOException e) {
      e.printStackTrace();
    }
    img_scaled.createGraphics().drawImage(img, 0, 0, 50, 50, null);
    profilePicLabel.setIcon(new ImageIcon(img_scaled));
  }

  public void setUserName(String username) {
    usernameLabel.setText(username);
  }
}