⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rutilities.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		Component c = parent.getComponent(row * cols + col);
		return layout.getConstraints(c);
	}


/*****************************************************************************/


	/**
	 * Returns a <code>String</code> of the form "#xxxxxx" good for use
	 * in HTML, representing the given color.
	 *
	 * @param color The color to get a string for.
	 * @return The HTML form of the color
	 * @throws NullPointerException If <code>color</code> is
	 *                              <code>null</code>.
	 */
	public static final String getHTMLFormatForColor(Color color) {
		String hexRed = Integer.toHexString(color.getRed());
		if (hexRed.length()==1)
			hexRed = "0" + hexRed;
		String hexGreen = Integer.toHexString(color.getGreen());
		if (hexGreen.length()==1)
			hexGreen = "0" + hexGreen;
		String hexBlue = Integer.toHexString(color.getBlue());
		if (hexBlue.length()==1)
			hexBlue = "0" + hexBlue;
		return "#" + hexRed + hexGreen + hexBlue;
	}


/*****************************************************************************/


	/**
	 * Returns the location of the specified jar file in the currently-running
	 * application's classpath.  This can be useful if you wish to know the
	 * location of the installation of the currently-running application.<p>
	 * For example, a Java program running from the executable jar
	 * <code>Foo.jar</code> can call this method with <code>Foo.jar</code> as
	 * the parameter, and the location of the jar file would be returned.  With
	 * this knowledge, along with knowledge of the directory layout of the
	 * application, the programmer can access other files in the installation.
	 *
	 * @param jarFileName The name of the jar file for which to search.
	 * @return The directory in which the jar file resides.
	 */
	public static final String getLocationOfJar(String jarFileName) {

		String classPath = System.getProperty("java.class.path");
		int index = classPath.indexOf(jarFileName);

		// A jar file on a classpath MUST be explicitly given; a jar file
		// in a directory, for example, will not be picked up by specifying
		// "-classpath /my/directory/".  So, we can simply search for the
		// jar name in the classpath string, and if it isn't there, it must
		// be in the current directory.
		if (index>-1) {
			int pathBeginning = classPath.lastIndexOf(File.pathSeparator,
												index-1) + 1;
			return classPath.substring(pathBeginning, index);
		}

		// Otherwise, it must be in the current directory.
		else {
			return System.getProperty("user.dir");
		}

	}


/*****************************************************************************/


	/**
	 * Returns a 24-bit integer representing the specified color.
	 *
	 * @return An integer representing the color.  This value is
	 *         calculated as <code>((red<<16) | (green<<8) | (blue))</code>.
	 * @see #getColorFromPacked
	 */
	public static int getPackedColor(Color c) {
		return (c.getRed()<<16) | (c.getGreen()<<8) | c.getBlue();
	}


/*****************************************************************************/


	/**
	 * This method is ripped off from <code>SpringUtilities.java</code> found
	 * on Sun's Java Tutorial pages.  It takes a component whose layout is
	 * <code>SpringLayout</code> and organizes the components it contains into
	 * a nice grid.
	 * Aligns the first <code>rows</code> * <code>cols</code> components of
	 * <code>parent</code> in a grid. Each component in a column is as wide as
	 * the maximum preferred width of the components in that column; height is
	 * similarly determined for each row.  The parent is made just big enough
	 * to fit them all.
	 *
	 * @param parent The container whose layout is <code>SpringLayout</code>.
	 * @param rows The number of rows of components to make in the container.
	 * @param cols The umber of columns of components to make.
	 * @param initialX The x-location to start the grid at.
	 * @param initialY The y-location to start the grid at.
	 * @param xPad The x-padding between cells.
	 * @param yPad The y-padding between cells.
	 */
	public static final void makeSpringCompactGrid(Container parent, int rows,
								int cols, int initialX, int initialY,
								int xPad, int yPad) {

		SpringLayout layout;
		try {
			layout = (SpringLayout)parent.getLayout();
		} catch (ClassCastException cce) {
			System.err.println("The first argument to makeCompactGrid " +
							"must use SpringLayout.");
			return;
		}

		//Align all cells in each column and make them the same width.
		Spring x = Spring.constant(initialX);
		for (int c = 0; c < cols; c++) {
			Spring width = Spring.constant(0);
			for (int r = 0; r < rows; r++) {
				width = Spring.max(width,
						getConstraintsForCell(
									r, c, parent, cols).getWidth());
			}
			for (int r = 0; r < rows; r++) {
				SpringLayout.Constraints constraints =
							getConstraintsForCell(r, c, parent, cols);
				constraints.setX(x);
				constraints.setWidth(width);
			}
			x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
		}

		//Align all cells in each row and make them the same height.
		Spring y = Spring.constant(initialY);
		for (int r = 0; r < rows; r++) {
			Spring height = Spring.constant(0);
			for (int c = 0; c < cols; c++) {
				height = Spring.max(height,
					getConstraintsForCell(r, c, parent, cols).getHeight());
			}
			for (int c = 0; c < cols; c++) {
				SpringLayout.Constraints constraints =
							getConstraintsForCell(r, c, parent, cols);
				constraints.setY(y);
				constraints.setHeight(height);
			}
			y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
		}

		//Set the parent's size.
		SpringLayout.Constraints pCons = layout.getConstraints(parent);
		pCons.setConstraint(SpringLayout.SOUTH, y);
		pCons.setConstraint(SpringLayout.EAST, x);

	}


/*****************************************************************************/


	/**
	 * Reads the text from a specified reader and returns it in a
	 * <code>String</code>.  This method is stolen from
	 * <code>DefaultEditorKit</code> and modified to save into a string
	 * rather than a <code>Document</code>.</p>
	 *
	 * Any of CR, LF, and CR/LF will be interpreted as the end-of-line
	 * marker if found, and will be transformed into <code>\n</code> in
	 * the string.
	 *
	 * @param in The reader from which to read.
	 * @return The text read from the reader.
	 * @throws IOException If an error occurs while reading.
	 */
	private static final int BUF_SIZE	= 16384;
	public static String read(Reader in) throws IOException {
	
		char[] buff = new char[BUF_SIZE];
		int nch;
		boolean lastWasCR = false;
		int last;
		StringBuffer stringBuffer = new StringBuffer();

		// Read in a block at a time, mapping \r\n to \n, as well as single
		// \r's to \n's. If a \r\n is encountered, \r\n will be set as the
		// newline string for the document, if \r is encountered it will
		// be set as the newline character, otherwise the newline property
		// for the document will be removed.
		while ((nch = in.read(buff, 0, buff.length)) != -1) {
			last = 0;
			for(int counter=0; counter<nch; counter++) {
				switch(buff[counter]) {
					case '\r':
						if (lastWasCR) {
							if (counter == 0) {
								stringBuffer.append("\n");
							}
							else {
								buff[counter - 1] = '\n';
							}
						}
						else {
							lastWasCR = true;
						}
						break;
					case '\n':
						if (lastWasCR) {
							if (counter > (last + 1))
								stringBuffer.append(buff,
										last,counter-last-1);
							// else nothing to do, can skip \r, next write will
							// write \n
							lastWasCR = false;
							last = counter;
						}
						break;
					default:
						if (lastWasCR) {
							if (counter == 0) {
								stringBuffer.append("\n");
							}
							else {
								buff[counter - 1] = '\n';
							}
							lastWasCR = false;
						}
						break;
				}
			}
			if (last < nch) {
				if(lastWasCR) {
					if (last < (nch - 1)) {
						stringBuffer.append(buff, last,nch-last-1);
					}
				}
				else {
					stringBuffer.append(buff, last,nch-last);
				}
			}
			if (lastWasCR) {
				stringBuffer.append("\n");
			}
		}
		return stringBuffer.toString();
	}


/*****************************************************************************/


	/**
	 * Enables or disables the specified component and all of its children,
	 * if it is a <code>java.awt.Container</code>.
	 *
	 * @param comp The component to enable/disable, along with its children, if
	 *             any.
	 * @param enabled Whether to enable or disable.
	 */
	public static void setChildrenEnabled(Component comp, boolean enabled) {
		comp.setEnabled(enabled);
		if(comp instanceof Container) {
			Container container = (Container)comp;
			int count = container.getComponentCount();
			for(int i=0; i<count; i++)
				setChildrenEnabled(container.getComponent(i), enabled);
		}
	}


/*****************************************************************************/


	/**
	 * Sets the accessible description on the specified component.
	 *
	 * @param comp The component on which to set the accessible description.
	 * @param bundle A resource bundle from which to get the description.
	 * @param key The key for the description in the resource bundle.
	 */
	public static void setDescription(JComponent comp, ResourceBundle bundle,
								String key) {
		comp.getAccessibleContext().setAccessibleDescription(
											bundle.getString(key));
	}


/*****************************************************************************/


	/**
	 * Sets the mnemonic on the specified button to be the object specified.
	 *
	 * @param b The button for which to set the mnemonic.
	 * @param mnemonic Either an <code>Integer</code> or a <code>String</code>
	 *        whose first character will be used as the mnemonic.
	 */
	public static final void setMnemonic(AbstractButton b, Object mnemonic) {
		if (mnemonic instanceof Integer)
			b.setMnemonic(((Integer)mnemonic).intValue());
		else if (mnemonic instanceof String)
			b.setMnemonic(((String)mnemonic).charAt(0));
	}


/*****************************************************************************/

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -