Ejemplo n.º 1
0
  public DistributedTextEditor() {
    area1.setFont(new Font("Monospaced", Font.PLAIN, 12));

    area2.setFont(new Font("Monospaced", Font.PLAIN, 12));
    ((AbstractDocument) area1.getDocument()).setDocumentFilter(dec);
    area2.setEditable(false);

    Container content = getContentPane();
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

    JScrollPane scroll1 =
        new JScrollPane(
            area1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    content.add(scroll1, BorderLayout.CENTER);

    JScrollPane scroll2 =
        new JScrollPane(
            area2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    content.add(scroll2, BorderLayout.CENTER);

    content.add(ipaddress, BorderLayout.CENTER);
    content.add(portNumber, BorderLayout.CENTER);

    JMenuBar JMB = new JMenuBar();
    setJMenuBar(JMB);
    JMenu file = new JMenu("File");
    JMenu edit = new JMenu("Edit");
    JMB.add(file);
    JMB.add(edit);

    file.add(Listen);
    file.add(Connect);
    file.add(Disconnect);
    file.addSeparator();
    file.add(Save);
    file.add(SaveAs);
    file.add(Quit);

    edit.add(Copy);
    edit.add(Paste);
    edit.getItem(0).setText("Copy");
    edit.getItem(1).setText("Paste");

    Save.setEnabled(false);
    SaveAs.setEnabled(false);
    Disconnect.setEnabled(false);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    area1.addKeyListener(k1);
    area1.addMouseListener(m1);
    setTitle("Disconnected");
    setVisible(true);
    area1.insert("Welcome to Hjortehandlerne's distributed text editor. \n", 0);

    this.addWindowListener(w1);
  }
Ejemplo n.º 2
0
 public ConsolePanel() {
   super(new BorderLayout());
   text.setFont(StyleContext.getDefaultStyleContext().getFont("SansSerif", Font.PLAIN, 10));
   JScrollPane scroller = new JScrollPane(text);
   scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
   add(BorderLayout.CENTER, scroller);
 }
  /** Constructor for the class, sets up two fresh tabbed text areas for program feedback. */
  public MessagesPane() {
    super();
    this.setMinimumSize(new Dimension(0, 0));
    assemble = new JTextArea();
    run = new JTextArea();
    assemble.setEditable(false);
    run.setEditable(false);
    // Set both text areas to mono font.  For assemble
    // pane, will make messages more readable.  For run
    // pane, will allow properly aligned "text graphics"
    // DPS 15 Dec 2008
    Font monoFont = new Font(Font.MONOSPACED, Font.PLAIN, 12);
    assemble.setFont(monoFont);
    run.setFont(monoFont);

    JButton assembleTabClearButton = new JButton("Clear");
    assembleTabClearButton.setToolTipText("Clear the Mars Messages area");
    assembleTabClearButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            assemble.setText("");
          }
        });
    assembleTab = new JPanel(new BorderLayout());
    assembleTab.add(createBoxForButton(assembleTabClearButton), BorderLayout.WEST);
    assembleTab.add(
        new JScrollPane(
            assemble,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED),
        BorderLayout.CENTER);
    assemble.addMouseListener(
        new MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            String text;
            int lineStart = 0;
            int lineEnd = 0;
            try {
              int line = assemble.getLineOfOffset(assemble.viewToModel(e.getPoint()));
              lineStart = assemble.getLineStartOffset(line);
              lineEnd = assemble.getLineEndOffset(line);
              text = assemble.getText(lineStart, lineEnd - lineStart);
            } catch (BadLocationException ble) {
              text = "";
            }
            if (text.length() > 0) {
              // If error or warning, parse out the line and column number.
              if (text.startsWith(ErrorList.ERROR_MESSAGE_PREFIX)
                  || text.startsWith(ErrorList.WARNING_MESSAGE_PREFIX)) {
                assemble.select(lineStart, lineEnd);
                assemble.setSelectionColor(Color.YELLOW);
                assemble.repaint();
                int separatorPosition = text.indexOf(ErrorList.MESSAGE_SEPARATOR);
                if (separatorPosition >= 0) {
                  text = text.substring(0, separatorPosition);
                }
                String[] stringTokens = text.split("\\s"); // tokenize with whitespace delimiter
                String lineToken = ErrorList.LINE_PREFIX.trim();
                String columnToken = ErrorList.POSITION_PREFIX.trim();
                String lineString = "";
                String columnString = "";
                for (int i = 0; i < stringTokens.length; i++) {
                  if (stringTokens[i].equals(lineToken) && i < stringTokens.length - 1)
                    lineString = stringTokens[i + 1];
                  if (stringTokens[i].equals(columnToken) && i < stringTokens.length - 1)
                    columnString = stringTokens[i + 1];
                }
                int line = 0;
                int column = 0;
                try {
                  line = Integer.parseInt(lineString);
                } catch (NumberFormatException nfe) {
                  line = 0;
                }
                try {
                  column = Integer.parseInt(columnString);
                } catch (NumberFormatException nfe) {
                  column = 0;
                }
                // everything between FILENAME_PREFIX and LINE_PREFIX is filename.
                int fileNameStart =
                    text.indexOf(ErrorList.FILENAME_PREFIX) + ErrorList.FILENAME_PREFIX.length();
                int fileNameEnd = text.indexOf(ErrorList.LINE_PREFIX);
                String fileName = "";
                if (fileNameStart < fileNameEnd
                    && fileNameStart >= ErrorList.FILENAME_PREFIX.length()) {
                  fileName = text.substring(fileNameStart, fileNameEnd).trim();
                }
                if (fileName != null && fileName.length() > 0) {
                  selectEditorTextLine(fileName, line, column);
                  selectErrorMessage(fileName, line, column);
                }
              }
            }
          }
        });

    JButton runTabClearButton = new JButton("Clear");
    runTabClearButton.setToolTipText("Clear the Run I/O area");
    runTabClearButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            run.setText("");
          }
        });
    runTab = new JPanel(new BorderLayout());
    runTab.add(createBoxForButton(runTabClearButton), BorderLayout.WEST);
    runTab.add(
        new JScrollPane(
            run,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED),
        BorderLayout.CENTER);
    this.addTab("Mars Messages", assembleTab);
    this.addTab("Run I/O", runTab);
    this.setToolTipTextAt(
        0,
        "Messages produced by Run menu. Click on assemble error message to select erroneous line");
    this.setToolTipTextAt(1, "Simulated MIPS console input and output");
  }
  /** Constructor to create the frame and its components */
  public CoreyTextEditor() {
    // Create a scroll pane
    area.setFont(new Font("Monospaced", Font.PLAIN, 12));
    JScrollPane scroll =
        new JScrollPane(
            area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    add(scroll, BorderLayout.CENTER);

    // Adds the system default look and feel

    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException
        | InstantiationException
        | UnsupportedLookAndFeelException
        | IllegalAccessException e) {
      e.printStackTrace();
    }

    // Create a menu bar
    JMenuBar JMB = new JMenuBar();
    setJMenuBar(JMB);
    JMenu file = new JMenu("File");
    JMenu edit = new JMenu("Edit");
    JMB.add(file);
    JMB.add(edit);

    // Finishing our menu bar
    file.add(New);
    file.add(Open);
    file.add(Save);
    file.add(SaveAs);
    file.addSeparator();
    file.add(Quit);

    edit.add(Cut);
    edit.add(Copy);
    edit.add(Paste);

    edit.getItem(0).setText("Cut");
    edit.getItem(0).setIcon(new ImageIcon("cut.gif"));
    edit.getItem(1).setText("Copy");
    edit.getItem(1).setIcon(new ImageIcon("copy.gif"));
    edit.getItem(2).setText("Paste");
    edit.getItem(2).setIcon(new ImageIcon("paste.gif"));

    // Time to make a toolbar!
    JToolBar tool = new JToolBar();
    add(tool, BorderLayout.NORTH);
    tool.add(New);
    tool.add(Open);
    tool.add(Save);
    tool.addSeparator();

    JButton cut = tool.add(Cut);
    JButton cop = tool.add(Copy);
    JButton pas = tool.add(Paste);

    cut.setText(null);
    cut.setIcon(new ImageIcon("cut.gif"));
    cop.setText(null);
    cop.setIcon(new ImageIcon("copy.gif"));
    pas.setText(null);
    pas.setIcon(new ImageIcon("paste.gif"));

    Save.setEnabled(false);
    SaveAs.setEnabled(false);

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    pack();

    /*
     KeyListener to change Save and SaveAs
    */
    KeyListener k1 =
        new KeyAdapter() {
          public void keyPressed(KeyEvent e) {
            changed = true;
            Save.setEnabled(true);
            SaveAs.setEnabled(true);
          }
        };
    area.addKeyListener(k1);
    setTitle(currentFile + " - CoreyTextEditor");
    setVisible(true);
  }