Friday, February 6, 2015

RadRibbonBar Application Menu | Telerik Winforms 2010 SP1 - RibbonBar - Hide the Application Menu Button

RadRibbonBar Default View

Telerik Winforms provides a RibbonBar control you can build user interfaces similar to those used in Microsoft Office 2007. The control has an Application Menu Button which is located on the upper left corner as shown below:


Sometimes you may want to hide the Application Menu Button. If you change its size to 0, you will get an error. While if you change its size to 1 (the minimal possible value), you will get something like:

Remove Application Menu Button From RadRibbonBar

To completely hide the button, try this:

this.RibbonBarElement.ApplicationButtonElement.Visibility = ElementVisibility.Collapsed;


Create a Successful Online Store at Bigcommerce! Try it Free Now!

Wednesday, February 4, 2015

RadGridView Multiple Columns Sorting | Telerik Winforms 2010 SP1 - RadGridView - Multiple Columns Sorting

RadGridView supports multiple columns sorting in three ways:

RadGridView Multiple Columns Sorting at Design Time

At design time, you can enable multiple columns sorting following these steps:
  1. Open designer view in Visual Studio;
  2. Right click and then select "Open Property Builder" in the context menu;
  3. In the property builder, select "GridViewTemplate" on the left pane;
  4. Select "Advanced" tab on the right pane;
  5. Search for "SortExpressions" in the property window below and then click the button on the right;
  6. In the "GridSortField Collection Editor" dialog, add columns to be sorted.

RadGridView Multiple Columns Sorting By Program

You can enable multiple columns sorting programmatically; following these steps described here

RadGridView Multiple Columns Sorting at Runtime

At runtime, you can enable multiple columns sorting by holding "Shift" key and click on the headers of the columns to be sorted.
gridview-sorting-setting-sorting-programmatically 001



Create a Successful Online Store at Bigcommerce! Try it Free Now!

C# - Convert Java Time (in Milliseconds) to Windows Time

Assume you have a frontend built with C# and a backend built with Java. In a grid view you want to display some data from a database table and one column uses numeric format to store date/time values.

If you simply do like the following way, you will get wrong answer.
  DateTime dateTime = DateTime.FromFileTime(javaTime);

  return dateTime.ToString("yyyy-MM-dd HH:mm:ss");

To get correct answer, use this code:

private static DateTime WINDOWS_EPOCH = new DateTime(1601, 1, 1, 0, 0, 0);
private static DateTime JAVA_EPOCH = new DateTime(1970, 1, 1, 0, 0, 0);

public static String FromJavaTime(long javaTime, string timeFormat)
{
  long epochDiff = (JAVA_EPOCH.ToFileTimeUtc() - WINDOWS_EPOCH.ToFileTimeUtc()) / TimeSpan.TicksPerMillisecond;

  DateTime dateTime = DateTime.FromFileTime((javaTime + epochDiff) * TimeSpan.TicksPerMillisecond);

  return dateTime.ToString(timeFormat);
}

Then if you run FromJavaTime(1420621059959, "yyyy-MM-dd HH:mm:ss"), you will get "2015-01-07 16:57:39".



Create a Successful Online Store at Bigcommerce! Try it Free Now!

Microsoft Excel - Create A Mulplication Table

The formula that you need for the multiplication table is a formula that will multiply whatever is in row 1 by whatever is in column A.
To have a reference that always points to row 1, use something in the format of X$1. To have a reference that points to column A, use a reference in the format of $An.

First, create something like:

Then enter $A2*B$1 into B2.

Next, copy the formula in B2 to the entire area and you will get the expected output:



Create a Successful Online Store at Bigcommerce! Try it Free Now!

Tuesday, February 3, 2015

Ganymed SSH2 Cisco Router | Using Ganymed SSH2 to Configure Cisco Routes/Switches

Configure Cisco Routers/Switches Using SSH

I have some use cases where SSH is used to configure Cisco routers/switches. For example, to enable a WAN interface, the following commands are executed:
en
password
configure terminal 
int Fa0/0/0
ip address 172.0.0.1255.255.255.252
description Link to 408
bandwidth 8000
service-policy output shape-8Mb
no shutdown
end

copy run start
exit
 

Apache Camel SSH Component

Apache Camel has a SSH component. However, I found that it did not work when configuring Cisco routers/switches.
The problem is that it cannot execute the "en password" command.

Ganymed SSH2 Library

Fortunately I could solve it by using another SSH library called Ganymed.
The following is the code I used to execute IOS commands.

 



private static final String PROMPT_SYMBOL = "%";
private static final char ERROR_SYMBOL = '^';
private static final String ACCESS_DENIED_MESSAGE = "Access denied";
private static final int MAX_OUTPUT_BUFFER_SIZE = 1024 * 1024;

public void executeCommand(String host, String username, String password, int timeout, String command) throws IOException, AccessDeniedException
{
  Connection connection = new Connection(host);
  
  connection.connect(null, timeout, timeout);
  
  boolean authenticated = connection.authenticateWithPassword(username, password);
  
  if (!authenticated)
    return;
  
  Session session = connection.openSession();
  session.startShell();
  
  executeCommand(session, timeout, command);
}
Hotels.com 
private SshResponse executeCommand(Session session, int timeout, String command) throws IOException, AccessDeniedException
{
  OutputStream in = session.getStdin();

  in.write(command.getBytes());
  
  InputStream stdout = session.getStdout();
  InputStream stderr = session.getStderr();
  
  StringBuilder sb = new StringBuilder();
  boolean valid = false;
  String line = null;

  boolean flag = true;
  while (flag)
  {
    int conditions = session.waitForCondition(ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA | ChannelCondition.EOF, timeout);

    if ((conditions & ChannelCondition.TIMEOUT) != 0)
    {
      break;
    }

    if ((conditions & ChannelCondition.EOF) != 0)
    {
      if ((conditions & (ChannelCondition.STDOUT_DATA | ChannelCondition.STDERR_DATA)) == 0)
      {
        break;
      }
    }

    BufferedReader reader = null;
    try
    {
      if ((ChannelCondition.STDOUT_DATA & conditions) != 0)
      {
        reader = new BufferedReader(new InputStreamReader(stdout));
      }
      else
      {
        reader = new BufferedReader(new InputStreamReader(stderr));
      }

      //Reader.readLine() may hang if 'exit' is not added to the command as the last line (use the 'exit' to exit the shell)
      
      boolean toAppend = true;
      
      while (true)
      {
        line = reader.readLine();
        
        if (line == null)
        {
          valid = true;
          
          flag = false;
          break;
        }
        
        if (toAppend)
        {
          toAppend = this.append(sb, line);
        }
        
        if (line.trim().startsWith(PROMPT_SYMBOL))
        {
          if (line.trim().indexOf(ERROR_SYMBOL) >= 0)
          {
            valid = false;
            
            flag = false;
            break;
          }
          else if (line.trim().contains(ACCESS_DENIED_MESSAGE))
          {
            throw new AccessDeniedException(line.trim());
          }
        }
        
        line = reader.readLine();
      }
    }
    finally
    {
      if (reader != null)
        reader.close();
    }
  }
  
  String message = sb.toString().trim();
  
  SshResponse response = new SshResponse();
  
  response.setCommand(command);
  response.setValid(valid);
  response.setOut(message);
  //keep all output
  //r4(config)#int Fastethernet1/0
  //               ^
  //% Invalid input detected at '^' marker.
  response.setErr(message);
  
  return response;
}
private boolean append(StringBuilder sb, String line)
{
  if (sb.length() >= MAX_OUTPUT_BUFFER_SIZE)
  {
    sb.setLength(MAX_OUTPUT_BUFFER_SIZE - 3);
    sb.append("...");
    
    return false;
  }
  
  if (sb.length() + line.length() > MAX_OUTPUT_BUFFER_SIZE)
  {
    //Minimum abbreviation width is 4
    if (MAX_OUTPUT_BUFFER_SIZE - sb.length() < 4)
    {
      sb.setLength(MAX_OUTPUT_BUFFER_SIZE - 3);
      sb.append("...");
    }
    else
    {
      String abbreviated = StringUtils.abbreviate(line, MAX_OUTPUT_BUFFER_SIZE - sb.length());
      
      sb.append(abbreviated);
    }
    
    sb.append('\n');
    
    return false;
  }
  
  sb.append(line);
  sb.append('\n');
  
  return true;
}

Keep in mind that the last command "exit" should not be omitted otherwise the SSH session may hang up for ever.


Create a Successful Online Store at Bigcommerce! Try it Free Now!