/* * (non-Javadoc) * * @see dao.MapDAO#deleteMap(components.RoomMap) */ @Override public void deleteMap(RoomMap mapToDelete) { for (RoomMap map : allMaps) { if (map.getId() == mapToDelete.getId()) { allMaps.remove(map); } } }
/** * Instantiates a new <code>MapPreviewPanel</code>. This constructor is used when we open <code> * AddMapDialog</code> in <code>AddMapDialogMode.EDIT</code> mode. * * @param map <code>RoomMap</code> object * @param parent <code>AddMapDialog</code> parent object * @see AddMapDialog * @see gui.enumeration.AddMapDialogMode */ public MapPreviewPanel(RoomMap map, AddMapDialog parent) { receiverViews = new ArrayList<ReceiverView>(); scalingRatioToFitContainer = 1.0; this.map = map; // TODO: added code Application.getApplication().setRoomMap(this.map); this.originalBackgroundImage = Utilities.deepCopy((BufferedImage) map.getImage()); this.backgroundImage = Utilities.deepCopy((BufferedImage) map.getImage()); this.parent = parent; initializeGui(); }
/** * Gets the validation error messages. * * @return <code>List</code> of validation error messages */ public List<String> getValidationErrorMessages() { ArrayList<String> validationErrors = new ArrayList<>(); // check if map has an image if (map.getImage() == null) { validationErrors.add(NO_IMAGE_ERROR_MESSAGE); } // check if map markers are in valid positions // we take the lower left corner of the lowerLeftMarker as the marker position point int lowerLeftMarkerPositionX = (int) (lowerLeftMarker.getLocation().getX() / scalingRatioToFitContainer); int lowerLeftMarkerPositionY = (int) (lowerLeftMarker.getLocation().getY() / scalingRatioToFitContainer) + (CoordinateZeroMarkerView.ZERO_COORDINATE_MARKER_VIEW_HEIGHT); // we take the upper right corner of the upperRightMarker as the marker position point int upperRightMarkerPositionX = (int) (upperRightMarker.getLocation().getX() / scalingRatioToFitContainer) + (CoordinateZeroMarkerView.ZERO_COORDINATE_MARKER_VIEW_WIDTH); int upperRightMarkerPositionY = (int) (upperRightMarker.getLocation().getY() / scalingRatioToFitContainer); if ((lowerLeftMarkerPositionX > upperRightMarkerPositionX) || (lowerLeftMarkerPositionY < upperRightMarkerPositionY)) { validationErrors.add(INVALID_POSITION_OF_MARKERS_ERROR_MESSAGE); } // check if receivers are outside the map for (ReceiverView receiverView : receiverViews) { // we take the middle point of receiverView as the receiver position point int receiverViewLocationX = (int) (receiverView.getLocation().getX() / scalingRatioToFitContainer) + (ReceiverView.RECEIVER_ITEM_WIDTH / 2); int receiverViewLocationY = (int) (receiverView.getLocation().getY() / scalingRatioToFitContainer) + (ReceiverView.RECEIVER_ITEM_HEIGHT / 2); if ((receiverViewLocationX < lowerLeftMarkerPositionX) || (receiverViewLocationX > upperRightMarkerPositionX) || (receiverViewLocationY > lowerLeftMarkerPositionY) || (receiverViewLocationY < upperRightMarkerPositionY)) { validationErrors.add( RECEIVERS_OUT_OF_MAP_ERROR_MESSAGE + " Please check receiver " + receiverView.getReceiver().getID()); break; } } return validationErrors; }
/** * Initializes graphical user interface. Layout of this panel is set to <code>null</code> (no * <code>LayoutManager</code>) so that <code>ReceiverView</code> items can be positioned * absolutely. * * @see ReceiverView * @see LayoutManager */ private void initializeGui() { logger = Utilities.initializeLogger(this.getClass().getName()); setSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT)); setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT)); setBackground(new Color(GRAY_COLOUR, GRAY_COLOUR, GRAY_COLOUR)); setLayout(null); // in order to position ReceiverViews absolutely refreshPreviewImage(); // register all receiver views to the ComponentMover componentMover = new ComponentMover(); // add zero coordinate marker views addCoordinateZeroMarkerViewsToMap(); for (Receiver receiver : map.getReceivers()) { ReceiverView receiverView = new ReceiverView(receiver, this); receiverViews.add(receiverView); this.add(receiverView); // TODO: location should be calculated with offsets for scaling // the image and pixel/meter scaling double receiverPositionInMetersX = receiver.getXPos(); double receiverPositionInMetersY = receiver.getYPos(); double mapRatioWidth = map.getRatioWidth(); double mapRatioHeight = map.getRatioHeight(); int lowerLeftMarkerOffsetXInPixels = map.getLowerLeftMarkerOffsetXInPixels(); int lowerLeftMarkerOffsetYInPixels = map.getLowerLeftMarkerOffsetYInPixels(); int receiverPositionInPixelsX = calculateReceiverPositionInPixelsX( receiverPositionInMetersX, lowerLeftMarkerOffsetXInPixels, mapRatioWidth); int receiverPositionInPixelsY = calculateReceiverPositionInPixelsY( receiverPositionInMetersY, lowerLeftMarkerOffsetYInPixels, mapRatioHeight); receiverView.setLocation(receiverPositionInPixelsX, receiverPositionInPixelsY); componentMover.registerComponent(receiverView); } }
/* * (non-Javadoc) * * @see dao.MapDAO#saveMap(components.RoomMap) */ @Override public void saveMap(RoomMap newMap) { // if map already existed and was modified, replace the old map with new one int listSize = allMaps.size(); for (int i = 0; i < listSize; i++) { if (allMaps.get(i).getId() == newMap.getId()) { allMaps.set(i, newMap); break; } } // send the single map to the server uploadMapToServer(newMap); }
/** Adds <code>CoordinateZeroMarkerView</code>s to <code>RoomMap</code>. */ public void addCoordinateZeroMarkerViewsToMap() { if (lowerLeftMarker == null || upperRightMarker == null) { lowerLeftMarker = new CoordinateZeroMarkerView(CoordinateZeroMarkerViewType.LOWER_LEFT, this); upperRightMarker = new CoordinateZeroMarkerView(CoordinateZeroMarkerViewType.UPPER_RIGHT, this); if (map.getLowerLeftMarkerOffsetXInPixels() != 0 || map.getLowerLeftMarkerOffsetYInPixels() != 0) { lowerLeftMarker.setLocation( (int) (map.getLowerLeftMarkerOffsetXInPixels() * scalingRatioToFitContainer), ((int) (map.getLowerLeftMarkerOffsetYInPixels() * scalingRatioToFitContainer) - (int) (CoordinateZeroMarkerView.ZERO_COORDINATE_MARKER_VIEW_HEIGHT * scalingRatioToFitContainer))); } if (map.getUpperRightMarkerOffsetXInPixels() != 0 || map.getUpperRightMarkerOffsetYInPixels() != 0) { upperRightMarker.setLocation( ((int) (map.getUpperRightMarkerOffsetXInPixels() * scalingRatioToFitContainer) - (int) (CoordinateZeroMarkerView.ZERO_COORDINATE_MARKER_VIEW_WIDTH * scalingRatioToFitContainer)), (int) (map.getUpperRightMarkerOffsetYInPixels() * scalingRatioToFitContainer)); } add(lowerLeftMarker); add(upperRightMarker); lowerLeftMarker.repaint(); upperRightMarker.repaint(); revalidate(); componentMover.registerComponent(lowerLeftMarker); componentMover.registerComponent(upperRightMarker); } else { // don't add another pair of zero coordinate map marker views, just set position to zero and // repaint the old // ones lowerLeftMarker.setLocation(0, 0); lowerLeftMarker.repaint(); upperRightMarker.setLocation(0, 0); upperRightMarker.repaint(); revalidate(); } }
/* * (non-Javadoc) * * @see dao.MapDAO#addMap(components.RoomMap) */ @Override public void addMap(RoomMap newMap) { // TODO find the index that doesn't exist and assign it to the map before storing it int newMapId = 0; int listSize = allMaps.size(); int highestId = 0; for (int i = 0; i < listSize; i++) { if (allMaps.get(i).getId() > highestId) { highestId = allMaps.get(i).getId(); } } newMapId = highestId + 1; newMap.setId(newMapId); allMaps.add(newMap); // send the single map to the server uploadMapToServer(newMap); }
/** * Removes the <code>ReceiverView</code> object from <code>RoomMap</code>. * * @param receiver <code>Receiver</code> object */ public void removeReceiverViewFromMap(Receiver receiver) { for (ReceiverView receiverView : receiverViews) { if (receiverView.getReceiver().getID() == receiver.getID()) { // remove receiver from map and receiverViews list this.remove(receiverView); receiverViews.remove(receiverView); componentMover.deregisterComponent(receiverView); repaint(); // set receivers coordinates to 0,0 for (Receiver receiverItem : map.getReceivers()) { if (receiverItem.getID() == receiver.getID()) { receiverItem.setOnMap(false); receiverItem.setxPos(0.0); receiverItem.setyPos(0.0); } } return; } } }
/** * Getter for <code>RoomMap</code> object with all its properties, including <code>Receiver</code> * s and <code>CoordinateZeroMarkerView</code>s . * * @return the map * @see Receiver * @see ReceiverView * @see CoordinateZeroMarkerView */ public RoomMap getMap() { // set zero coordinate marker positions int lowerLeftMarkerOffsetXInPixels = (int) (lowerLeftMarker.getLocation().getX() / scalingRatioToFitContainer); int lowerLeftMarkerOffsetYInPixels = (int) (lowerLeftMarker.getLocation().getY() / scalingRatioToFitContainer) + (CoordinateZeroMarkerView.ZERO_COORDINATE_MARKER_VIEW_HEIGHT); map.setLowerLeftMarkerOffsetXInPixels(lowerLeftMarkerOffsetXInPixels); map.setLowerLeftMarkerOffsetYInPixels(lowerLeftMarkerOffsetYInPixels); int upperRightMarkerOffsetXInPixels = (int) (upperRightMarker.getLocation().getX() / scalingRatioToFitContainer) + (CoordinateZeroMarkerView.ZERO_COORDINATE_MARKER_VIEW_WIDTH); int upperRightMarkerOffsetYInPixels = (int) (upperRightMarker.getLocation().getY() / scalingRatioToFitContainer); map.setUpperRightMarkerOffsetXInPixels(upperRightMarkerOffsetXInPixels); map.setUpperRightMarkerOffsetYInPixels(upperRightMarkerOffsetYInPixels); // set room width and height in meters double roomWidthInMeters = parent.getRoomWidthInMeters(); double roomHeightInMeters = parent.getRoomHeightInMeters(); map.setWidthInMeters(roomWidthInMeters); map.setHeightInMeters(roomHeightInMeters); // set width and height ratios int mapWidthInPixels = upperRightMarkerOffsetXInPixels - lowerLeftMarkerOffsetXInPixels; int mapHeightInPixels = lowerLeftMarkerOffsetYInPixels - upperRightMarkerOffsetYInPixels; double mapWidthRatio = (mapWidthInPixels) / roomWidthInMeters; double mapHeightRatio = (mapHeightInPixels) / roomHeightInMeters; map.setRatioWidth(mapWidthRatio); map.setRatioHeight(mapHeightRatio); // set receiver positions in meters map.getReceivers().clear(); // first we remove all present receivers for (ReceiverView receiverView : receiverViews) { int receiverViewXInPixels = ((int) (receiverView.getLocation().getX() / scalingRatioToFitContainer)) + (ReceiverView.RECEIVER_ITEM_WIDTH / 2); int receiverViewYInPixels = ((int) (receiverView.getLocation().getY() / scalingRatioToFitContainer)) + (ReceiverView.RECEIVER_ITEM_HEIGHT / 2); double receiverPositionInMetersX = calculateReceiverPositionInMetersX( lowerLeftMarkerOffsetXInPixels, receiverViewXInPixels, mapWidthRatio); double receiverPositionInMetersY = calculateReceiverPositionInMetersY( lowerLeftMarkerOffsetYInPixels, receiverViewYInPixels, mapHeightRatio); receiverView.getReceiver().setxPos(receiverPositionInMetersX); receiverView.getReceiver().setyPos(receiverPositionInMetersY); map.addReceiver(receiverView.getReceiver()); } // set map title String titleFromInput = parent.getRoomTitle(); map.setTitle(titleFromInput.equals("") ? "Unkown" : titleFromInput); // set map xFrom, xTo, yFrom and yTo values map.setxFrom(0); map.setxTo(map.getWidthInMeters()); map.setyFrom(0); map.setyTo(map.getHeightInMeters()); Application.getApplication().setRoomMap(map); return map; }
/* * (non-Javadoc) * * @see dao.MapDAO#getAllMaps() */ @Override public List<RoomMap> getAllMaps() { if (allMaps != null) { return allMaps; } else { allMaps = new ArrayList<RoomMap>(); } // sample maps initialization values RoomMap map = null; BufferedImage image = null; double roomWidth = 0.0; double roomHeight = 0.0; String title = ""; ArrayList<Receiver> receivers = null; // add sample map, id 0 String path1 = "images/room_4m_6m.png"; image = (BufferedImage) Utilities.loadImage(path1); title = "Test Room"; roomWidth = 4.0; roomHeight = 6.0; receivers = new ArrayList<Receiver>(); receivers.add(new Receiver(0, 0.0, 0.0, 45.0, true)); receivers.add(new Receiver(1, 4.0, 0.0, 135.0, true)); receivers.add(new Receiver(2, 4.0, 6.0, 225.0, true)); receivers.add(new Receiver(3, 0.0, 6.0, 315.0, true)); map = new RoomMap(image, title, receivers); map.setId(0); map.setWidthInMeters(roomWidth); map.setHeightInMeters(roomHeight); map.setLowerLeftMarkerOffsetXInPixels(0); map.setLowerLeftMarkerOffsetYInPixels(460); map.setUpperRightMarkerOffsetXInPixels(309); map.setUpperRightMarkerOffsetYInPixels(0); map.setRatioWidth(75.5); map.setRatioHeight(75.5); map.setPath(path1); allMaps.add(map); // add sample map, id 1 String path2 = "images/test_room_fifth_floor.png"; image = (BufferedImage) Utilities.loadImage(path2); title = "Room 501"; roomWidth = 6.0; roomHeight = 6.0; receivers = new ArrayList<Receiver>(); receivers.add(new Receiver(0, 0.0, 6.0, 315.0, true)); receivers.add(new Receiver(1, 2.0, 6.0, 270.0, true)); receivers.add(new Receiver(2, 6.0, 6.0, 225.0, true)); receivers.add(new Receiver(3, 0.0, 2.45, 0.0, true)); receivers.add(new Receiver(4, 6.0, 2.45, 180.0, true)); receivers.add(new Receiver(6, 6.0, 0.0, 135.0, true)); receivers.add(new Receiver(9, 3.60, 2.0, 270.0, true)); map = new RoomMap(image, title, receivers); map.setId(1); map.setWidthInMeters(roomWidth); map.setHeightInMeters(roomHeight); map.setLowerLeftMarkerOffsetXInPixels(0); map.setLowerLeftMarkerOffsetYInPixels(538); map.setUpperRightMarkerOffsetXInPixels(538); map.setUpperRightMarkerOffsetYInPixels(0); map.setRatioWidth(89.67); map.setRatioHeight(89.67); map.setPath(path2); allMaps.add(map); // add sample map, id 2 String path3 = "images/room_4m_6m.png"; image = (BufferedImage) Utilities.loadImage(path3); title = "Room 481"; roomWidth = 5.0; roomHeight = 8.30; receivers = new ArrayList<Receiver>(); receivers.add(new Receiver(6, 0.7, 0.0, 90.0, true)); receivers.add(new Receiver(1, 5.0, 0.65, 135.0, true)); receivers.add(new Receiver(2, 5.0, 4.5, 180.0, true)); receivers.add(new Receiver(3, 0.0, 2.0, 0.0, true)); receivers.add(new Receiver(4, 0.0, 7.8, 315.0, true)); receivers.add(new Receiver(0, 2.5, 8.3, 270.0, true)); receivers.add(new Receiver(9, 4.80, 8.3, 225.0, true)); map = new RoomMap(image, title, receivers); map.setId(2); map.setWidthInMeters(roomWidth); map.setHeightInMeters(roomHeight); map.setLowerLeftMarkerOffsetXInPixels(0); map.setLowerLeftMarkerOffsetYInPixels(460); map.setUpperRightMarkerOffsetXInPixels(309); map.setUpperRightMarkerOffsetYInPixels(0); map.setRatioWidth(61.80); map.setRatioHeight(55.42); map.setPath(path3); allMaps.add(map); return allMaps; }
private void uploadMapToServer(RoomMap newMap) { Mongo mongo = null; DB database; DBCollection sampleData; String databaseAddress = DEFAULT_DATABASE_ADDRESS; try { // read parameters from the configuration file databaseAddress = Utilities.getConfigurationValue(DATABASE_ADDRESS); } catch (NumberFormatException exception) { // reading has failed, use default values logger.info( "Reading parameters from configuration file failed. " + "Using default values for database_address instead."); } try { mongo = new Mongo(databaseAddress); } catch (UnknownHostException e1) { e1.printStackTrace(); } database = mongo.getDB("rssiSystem"); sampleData = database.getCollection("map_records"); // remove all maps sampleData.drop(); SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); String strDate = simpledateformat.format(new Date()); // ------------------- // Load our image byte[] imageBytes = null; try { imageBytes = Utilities.LoadImageAsBytes(newMap.getPath()); } catch (Exception e1) { e1.printStackTrace(); } // --------------------- // Create GridFS object GridFS fs = new GridFS(database); // Save image into database GridFSInputFile in = fs.createFile(imageBytes); in.save(); Object mapIdObject = in.getId(); // System.out.println(mapIdObject); try { DBObject documentDetail = new BasicDBObject(); documentDetail.put("_cls", "mapRecords"); // for mongoEngine ORM users documentDetail.put("image", mapIdObject); documentDetail.put("mapId", newMap.getId()); documentDetail.put("width", newMap.getWidthInMeters()); documentDetail.put("height", newMap.getHeightInMeters()); documentDetail.put("offsetX", newMap.getLowerLeftMarkerOffsetXInPixels()); documentDetail.put("offsetY", newMap.getLowerLeftMarkerOffsetYInPixels()); documentDetail.put("offset2X", newMap.getUpperRightMarkerOffsetXInPixels()); documentDetail.put("offset2Y", newMap.getUpperRightMarkerOffsetYInPixels()); documentDetail.put("scalingX", newMap.getRatioWidth()); documentDetail.put("scalingY", newMap.getRatioHeight()); documentDetail.put("title", newMap.getTitle()); documentDetail.put("updateTime", strDate); sampleData.insert(documentDetail); } catch (Exception e) { e.printStackTrace(); } }