/// Check for button clicking and apply appropriate response
    public void actionPerformed(ActionEvent e) {
      /// For each input, pass it to the correct handler
      /// Priority of router will be updated based on input
      /// Upon user's changes of the priority value, repaint
      /// the display component
      if (e.getSource() == aPlus) {
        Router temp = statMux.getRouter(indexR);
        int[] pList = temp.getPriorityList();
        pList[0] = dataBound(pList[0] + 1);
        temp.setPriorityList(pList);
        audioVal.setText(String.valueOf(pList[0]));
        videoVal.setText(String.valueOf(pList[1]));
        textVal.setText(String.valueOf(pList[2]));
        drawTest.repaint();
      }

      if (e.getSource() == aMinus) {
        Router temp = statMux.getRouter(indexR);
        int[] pList = temp.getPriorityList();
        pList[0] = dataBound(pList[0] - 1);
        temp.setPriorityList(pList);
        audioVal.setText(String.valueOf(pList[0]));
        videoVal.setText(String.valueOf(pList[1]));
        textVal.setText(String.valueOf(pList[2]));
        drawTest.repaint();
      }

      if (e.getSource() == vPlus) {
        Router temp = statMux.getRouter(indexR);
        int[] pList = temp.getPriorityList();
        pList[1] = dataBound(pList[1] + 1);
        temp.setPriorityList(pList);
        audioVal.setText(String.valueOf(pList[0]));
        videoVal.setText(String.valueOf(pList[1]));
        textVal.setText(String.valueOf(pList[2]));
        drawTest.repaint();
      }

      if (e.getSource() == vMinus) {
        Router temp = statMux.getRouter(indexR);
        int[] pList = temp.getPriorityList();
        pList[1] = dataBound(pList[1] - 1);
        temp.setPriorityList(pList);
        audioVal.setText(String.valueOf(pList[0]));
        videoVal.setText(String.valueOf(pList[1]));
        textVal.setText(String.valueOf(pList[2]));
        drawTest.repaint();
      }

      if (e.getSource() == tPlus) {
        Router temp = statMux.getRouter(indexR);
        int[] pList = temp.getPriorityList();
        pList[2] = dataBound(pList[2] + 1);
        temp.setPriorityList(pList);
        audioVal.setText(String.valueOf(pList[0]));
        videoVal.setText(String.valueOf(pList[1]));
        textVal.setText(String.valueOf(pList[2]));
        drawTest.repaint();
      }

      if (e.getSource() == tMinus) {
        Router temp = statMux.getRouter(indexR);
        int[] pList = temp.getPriorityList();
        pList[2] = dataBound(pList[2] - 1);
        temp.setPriorityList(pList);
        audioVal.setText(String.valueOf(pList[0]));
        videoVal.setText(String.valueOf(pList[1]));
        textVal.setText(String.valueOf(pList[2]));
        drawTest.repaint();
      }
    }
  public void actionPerformed(ActionEvent e) {
    Object o = e.getSource();

    for (JRadioButton u : ub) {
      if (o == u) {
        mapTM.setUnitType(u.getText());
        System.out.println("Set unit type - " + u.getText());
        return;
      }
    }

    if (o == timer) {

      /*
      mmLabel.setText("Main Menu ("+(cdTime--)+")");

      if(cdTime == 0){
      	timer.stop();
      	gsButton.setText("Game Start");
      	mmLabel.setText("Main Menu");

      	ArrayList<Building> bArr = new ArrayList<Building>();

      	String temp = "Elixir Collector-24,8-960|Elixir Collector-31,8-960|Gold Mine-17,10-960|Elixir Collector-25,21-960|Elixir Collector-11,22-960";
      	String[] bs = temp.split("\\|");
      	for(String b : bs){
      		String[] bParts = b.split("-");
      		String[] cParts = bParts[1].split(",");
      		int x = Integer.parseInt(cParts[0].trim());
      		int y = Integer.parseInt(cParts[1].trim());

      		Building tb = new Building(bParts[0], new Coordinate(x,y));
      		tb.setHp(Integer.parseInt(bParts[2].trim()));

      		bArr.add(tb);
      	}



      	mapTM.setBuildings(bArr);
      	switchCards("TM");
      }
      */

      return;
    }

    if (o == gsButton) {

      if (timer.isRunning()) {
        // timer.stop();
        gsButton.setText("Game Start");
        mmLabel.setText("Main Menu");
        // sends the leave command
        client.sendUDP("leave~" + unameUDP);
        return;
      }
      // sends the joinlobby command
      client.sendUDP("joinlobby~" + unameUDP);
      // gsButton.setText("Game Stop");
      String serverResp = client.receiveUDP();

      if (serverResp.trim().equals("false")) {

        // place false handler here

      } else {

        String[] enemies = serverResp.trim().split(",");
        ArrayList<Building> bArr = new ArrayList<Building>();
        String mapConfig = getBaseConfig(enemies[0]);
        String[] bs = mapConfig.split("\\|");
        for (String b : bs) {

          String[] bParts = b.split("-");
          String[] cParts = bParts[1].split(",");
          int x = Integer.parseInt(cParts[0].trim());
          int y = Integer.parseInt(cParts[1].trim());

          Building tb = new Building(bParts[0], new Coordinate(x, y));
          tb.setHp(Integer.parseInt(bParts[2].trim()));
          bArr.add(tb);
        }

        mapTM.setBuildings(bArr);
        switchCards("TM");
      }

      // System.out.println(serverResp);
      // cdTime = 10;
      // timer.start();
      return;
    }

    if (o == logout) {
      client.sendMessage(new ChatMessage(ChatMessage.LOGOUT, ""));
      chatArea.setText("");

      switchCards("Login");
      return;
    }

    if (o == cmButton) {
      String baseConfig = getBaseConfig();
      System.out.println("base config: " + baseConfig);

      for (int i = 0; i < mapSize; i++) {
        for (int j = 0; j < mapSize; j++) {
          tiles[i][j].setValue("");
        }
      }

      if (!baseConfig.equals("")) {

        String[] bs = baseConfig.split("\\|");
        for (String b : bs) {
          String[] bParts = b.split("-");
          String[] cParts = bParts[1].split(",");
          int x = Integer.parseInt(cParts[0].trim());
          int y = Integer.parseInt(cParts[1].trim());

          int index = 0;
          for (int i = 0; i < bb.size(); i++) {
            if (bb.get(i).getText().split("-")[0].trim().equals(bParts[0])) {
              index = i;
              break;
            }
          }
          insertBuilding(y, x, index);
        }
      }

      switchCards("CM");

      return;
    }

    if (o == tmButton) {

      ArrayList<Building> bArr = new ArrayList<Building>();

      for (int i = 0; i < 40; i++) {
        for (int j = 0; j < 40; j++) {
          if (tiles[i][j].getValue().equals("") || tiles[i][j].getValue().contains("-")) {
            continue;
          }
          // weird part here
          bArr.add(new Building(tiles[i][j].getValue(), new Coordinate(j, i)));
        }
      }

      mapTM.setBuildings(bArr);

      switchCards("TM");
      return;
    }

    // if it the who is in button
    if (o == whoIsIn) {
      client.sendMessage(new ChatMessage(ChatMessage.WHOISIN, ""));
      return;
    }

    if (o == cmBack) {
      ArrayList<Building> bArr = new ArrayList<Building>();
      for (int i = 0; i < 40; i++) {
        for (int j = 0; j < 40; j++) {
          if (tiles[i][j].getValue().equals("") || tiles[i][j].getValue().contains("-")) {
            continue;
          }
          // weird part here
          bArr.add(new Building(tiles[i][j].getValue(), new Coordinate(j, i)));
        }
      }

      String temp = "mapdata~" + unameUDP + "~";
      int tileCount = 40;
      int dim = 600;
      int tileDim = (int) (dim / tileCount);
      int counter = 0;
      for (Building b : bArr) {
        int x, y, hp;
        x = b.getPos().getX() / tileDim;
        y = b.getPos().getY() / tileDim;
        hp = b.getHp();
        temp += b.getName() + "-" + x + "," + y + "-" + hp + "|";
        counter += 1;
      }
      if (counter > 0) {
        temp = temp.substring(0, temp.length() - 1); // removes the last '|'
      } else {
        temp += "none";
      }

      client.sendUDP(temp); // allows saving of the current state of the map into the user's account

      switchCards("Menu");

      return;
    }
    if (o == tmBack) {
      switchCards("Menu");

      return;
    }

    for (int i = 0; i < 40; i++) {
      for (int j = 0; j < 40; j++) {
        if (o == tiles[i][j]) {
          for (int k = 0; k < bb.size(); k++) {
            if (bb.get(k).isSelected()) {
              if (bb.get(k).getText().equals("Remove Building")) {
                removeBuilding(i, j);
                return;
              }

              insertBuilding(i, j, k);
              // JOptionPane.showMessageDialog(null, bb.get(k).getText());
              return;
            }
          }

          // JOptionPane.showMessageDialog(null, "i-"+i+" j-"+j);
          return;
        }
      }
    }

    // ok it is coming from the JTextField
    if (connected) {
      // just have to send the message
      client.sendMessage(new ChatMessage(ChatMessage.MESSAGE, chatField.getText()));
      chatField.setText("");
      return;
    }

    if (o == login) {
      // ok it is a connection request
      String username = usernameField.getText().trim();
      String password = passwordField.getText().trim();
      // empty username ignore it
      if (username.length() == 0) return;
      // empty serverAddress ignore it
      String server = tfServer.getText().trim();
      if (server.length() == 0) return;
      // empty or invalid port numer, ignore it
      String portNumber = tfPort.getText().trim();
      if (portNumber.length() == 0) return;
      int port = 0;
      try {
        port = Integer.parseInt(portNumber);
      } catch (Exception en) {
        return; // nothing I can do if port number is not valid
      }

      // try creating a new Client with GUI
      client = new Client(server, port, username, password, this);
      // test if we can start the Client
      if (!client.start()) return;

      unameUDP = username;

      switchCards("Menu");
      // fetching of the base_config string from the database

      chatField.setText("");
      chatArea.setText("");
    }
  }
Exemple #3
0
    public void actionPerformed(ActionEvent ae) {
      String cmd = ae.getActionCommand().intern();
      if (ae.getSource() == butChannel) {
        String pass = null;
        String channel = (String) cboChannels.getSelectedItem();
        if (channels.containsKey(channel) && ((Boolean) channels.get(channel)).booleanValue()) {
          String message = "Password for " + channel + "? (blank for no password)";
          pass =
              JOptionPane.showInputDialog(
                  (Component) ae.getSource(),
                  message,
                  "Join Channel",
                  JOptionPane.QUESTION_MESSAGE);
        }
        server.writeObject(new SD_Channel(false, channel, ("".equals(pass) ? null : pass)));
        showUserStatus = false;
      } else if (ae.getSource() == butCreate) {
        String channel =
            JOptionPane.showInputDialog(
                (Component) ae.getSource(),
                "Enter the name of the channel",
                "Create Channel",
                JOptionPane.INFORMATION_MESSAGE);
        if (channel == null) return;
        String message = "Password for " + channel + "? (blank for no password)";
        String pass =
            JOptionPane.showInputDialog(
                (Component) ae.getSource(), message, "Join Channel", JOptionPane.QUESTION_MESSAGE);
        server.writeObject(new SD_Channel(true, channel, ("".equals(pass) ? null : pass)));
        showUserStatus = false;
        // ---------- Invite a Specific User
      } else if (ae.getSource() == butInvite) {
        String invite =
            JOptionPane.showInputDialog(
                (Component) ae.getSource(),
                "Enter the name of the User to Invite",
                "Invite User",
                JOptionPane.INFORMATION_MESSAGE);
        if (invite == null) return;
        messageText.setText("\\invite " + invite);
        sendMessage();

      } else if (cmd == "whisper") {
        String user = (String) userList.getSelectedValue();
        if (user != null && !messageText.getText().equals("") && !user.equals(username)) {
          messageText.setText("\\whisper " + user + " " + messageText.getText());
          sendMessage();
        } else {
          error(
              "invalid user or no message, type a message below,"
                  + " select a user, and then whisper");
        }
      } else if (cmd == "private message") {
        String user = (String) userList.getSelectedValue();
        if (user != null && !user.equals(username)) {
          privates.newPrivate(user);
        } else {
          error("invalid user");
        }
      } else if (ae.getActionCommand().equals("ignore")) {
        String user = (String) userList.getSelectedValue();
        if (user != null) {
          ignore(user, false);
        } else {
          error("no user selected");
        }
      } else if (cmd == "clear ignore list") {
        ignores.clear();
        updateList();
        serverMessage("ignore list cleared");
      } else if (cmd == "kick user") {
        String user = (String) userList.getSelectedValue();
        if (user != null) {
          if (user.equals(username)) {
            error("cannot kick yourself");
          } else {
            server.writeObject(new SD_Kick(user));
          }
        } else {
          error("no user selected");
        }
      }
    }